1. forward  和  redirect区别
2. servlet的几个方法     doget  dopost区别
3. ArrayList里装了Person类对象,怎样根据Person类的属性年龄来排序
4. HashMap的键值是否可以是任意对象大家看看吧关于第三题,网上有原题,结果小弟没看到,其实我一回查下API就会了,只是个人太老实了,就说不会,真应该和主管说声我虽然不会,我会查API的。开个玩笑啦第四题我看了API,觉得可以是任意对象,但是如果是同种对象,搜索起来是不是更好些?  请牛人帮忙结果一下。当然了,也问了点设计模式J2EE的问题。不多说。

解决方案 »

  1.   

    1. forward  和  redirect区别
       很简单,不用说
    2. servlet的几个方法    doget  dopost区别
       很简单,不用说
    3. ArrayList里装了Person类对象,怎样根据Person类的属性年龄来排序
       实现compare接口,重新定义比较原则,用年龄来比较
    4. HashMap的键值是否可以是任意对象 
       值可以,键不可以,键必须是可比较的对象,换句话说是实现了equals的!
     
      以上是自己的理解,及以前老师提到的,大家多指教!
      

  2.   


    HashMap的键不可以么?equals比较的是hashCode的返回值吧,应该可以是任意对象吧。
      

  3.   

    hashMap是以键取值,中间肯定涉及到比较键值,那么就需要键是可比的,没有实现equals的类是通过地址取值,那么2个对象属性相等,hashMap是可以取值的,但是由于用地址比较,那么就不等了,所以也就取不到值!
      

  4.   

    如果没有自定义equals方法,默认都是用调用hashCode方法来比较,不同对象hashCode返回不同,理论上不具可比性但是完全是可以进行比较的。所以HashMap的键和值都可以是任意对象。
      

  5.   

    第四题:键可以是任意对象,因为参数就是Object.
    但是,要想表现出正确的行为,这个对象必须实现hashCode(),保证相同的对象具有相同的hashcode,不同的对象具有不同的hashCode。这个hashCode就是给这些容器使用的比较方法,通常int hashCode()与boolean equals(Object anObject)应该具有相同的表现
      

  6.   


    public class customer { /**
     * @param args
     */
    public static void main(String[] args) {
    //创建两个对象
    customer c1 = new customer(1, "haha");
    customer c2 = new customer(1, "haha");
    Map<customer, String> m = new HashMap<customer, String>();
    //把c1当作键值
    m.put(c1, "我是"+c1.name);

    //使用c2取c1
    String cname = (String)m.get(c2);
    System.out.println(cname);
    } public customer(int i, String n) {
    id = i;
    name = n;
    } int id;
    String name; @Override
    public int hashCode() {
    return (this.name + this.id).hashCode();
    } @Override
    public boolean equals(Object o) {
    if (this == o) {
    return true;
    }
    if (o instanceof customer) {
    customer anotherCustomer = (customer) o; return anotherCustomer.id == this.id
    && anotherCustomer.name.equals(this.name);
    }
    return false;
    }
    }这里的public boolean equals(Object o)没有使用
      

  7.   

    HashMap 的键值可以是任意对象。
      

  8.   

    应该是实现comparable接口吧?
    有两种方式:
    1.Person类直接实现comparable这个接口,那Person类本身就具有了比较的功能
    2.自己写一个类,实现comparator这个接口,那就是一个比较器,然后在Person类中排序的时候指定这个比较器就行了
      

  9.   

    4. HashMap的键值是否可以是任意对象 
      值可以,键不可以,键必须是可比较的对象,换句话说是实现了equals的! 应该是可比较的,实现comparable的
      

  10.   

    public class Person{
         String name;
         int age;public Person(String name,int age){
         this.name = name;
         this.age = age;}public int getAge() {
         return age;
    }public void setAge(int age) {
         this.age = age;
    }public String getName() {
         return name;
    }public void setName(String name) {
         this.name = name;
    }}
    2:Mycomparator.java-------------------------------
    //实现Comparator接口,也就是定义排序规则,你几乎可以定义任何规则
    package com.infoearth;
    import java.util.*;
    public class Mycomparator implements Comparator{    public int compare(Object o1,Object o2) {
            Person p1=(Person)o1;
            Person p2=(Person)o2; 
           if(p1.age<p2.age)
               return 1;
           else
              return 0;
           }}3:ListSort.java------------------------------------package com.infoearth;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;public class ListSort {
         public static void main(String[] args){
             ArrayList list = new ArrayList();
             list.add(new Person("lcl",28));
             list.add(new Person("fx",23));
             list.add(new Person("wqx",29));
             Comparator comp = new Mycomparator();
             Collections.sort(list,comp);  
              for(int i = 0;i<list.size();i++){
                 Person p = (Person)list.get(i);
                 System.out.println(p.getName());
            }      }}或者写成实现Comparator接口的匿名类Collections.sort(list);Collections.sort(list, new Comparator() {      public int compare(Object o1,Object o2) {
            Person p1=(Person)o1;
            Person p2=(Person)o2; 
           if(p1.age<p2.age)
               return 1;
           else
              return 0;
           }
        });