Java Collection 注意事项

来源:互联网 发布:开淘宝店铺的流程 编辑:程序博客网 时间:2024/06/18 11:00

 相互分享学习Collection接口遇到问题,直接步入正题。

看一下Test.javacode

 

public classTest {

   public static void main(String[] args) {

      Collection<Object>c = newArrayList<Object>();

      c.add("Hello");

      c.add(new Integer(10));

      c.add(new Double(10.1));

      c.add(new Name("yang","xiao"));

      System.out.println(c.size());//4

      System.out.println(c);//c.toString()

      c.remove("Hello");

      System.out.println("result:"+c.remove(new Name("yang","xiao")));// result :false

      System.out.println(c); //c.toString()

   }

}

 

class Name {

   private StringfirstName;

   private StringlastName;

 

   public String getFirstName() {

      returnfirstName;

   }

 

   public String getLastName() {

      return lastName;

   }

 

   public void setFirstName(StringfirstName) {

      this.firstName = firstName;

   }

 

   public String toString() {

      return "fName="+ firstName+ ", lName=" + lastName;

   }

 

   public void setLastName(StringlastName) {

      this.lastName = lastName;

   }

 

   public Name(String firstName,String lastName) {

      this.firstName = firstName;

      this.lastName = lastName;

   }

}

 

结果:

4

[Hello, 10, 10.1, fName=yang,lName=xiao]

result:false

[10, 10.1, fName=yang, lName=xiao]

 

 

为什么c.remove(newName("yang","xiao"))    Name移除false呢?继续往下面看。

 

 

接下来修改一下Test.java (添加两个方法 equals()和hashCode()方法) 

public classCopyOfTest {

   public static void main(String[] args){

      Collection<Object>c = newArrayList<Object>();

      c.add("Hello");

      c.add(new Integer(10));

      c.add(new Double(10.1));

      c.add(new Name("yang","xiao"));

      System.out.println(c.size());//4

      System.out.println(c);//c.toString()

      c.remove("Hello");

      System.out.println("result:"+c.remove(new Name("yang","xiao")));// result :true

      System.out.println(c);

   }

}

 

class Name {

   private StringfirstName;

   private StringlastName;

 

   public String getFirstName() {

      return firstName;

   }

 

   public String getLastName() {

      return lastName;

   }

 

   public void setFirstName(StringfirstName) {

      this.firstName = firstName;

   }

 

   public String toString() {

      return "fName="+ firstName+ ", lName=" + lastName;

   }

 

   public void setLastName(StringlastName) {

      this.lastName = lastName;

   }

 

   public Name(String firstName,String lastName) {

      this.firstName = firstName;

      this.lastName = lastName;

   }

   public boolean equals(Object obj) {

 

      if (objinstanceof Name) {

         Namename = (Name) obj;

         return (firstName.equals(name.firstName) &&lastName

                .equals(name.lastName));

      }

      returnsuper.equals(obj);

   }

 

   public int hashCode() {

      return lastName.hashCode();

   }

}

 

结果:

4

[Hello, 10, 10.1, fName=yang,lName=xiao]

result:true

[10, 10.1]

 

当我们添加了equals()和hashCode()

c.remove(new Name("yang","xiao"))   现在Name移除true

 

两者的差别就在于有没有equals()和hashCode();   

 

 

分析:

第一个Test.java

没有equals() 和hashCode() 

 

c.add(new Name("yang","xiao"))、c.remove(newName("yang","xiao")

两个Name("yang","xiao")不是同一个对象,因为Name类继承Object类且调用的是Object里面的equals()和hashCode(),

Object判断两个对象相等

例如:只有两个对象的引用一样才返回真  

String x=”1”;

String y=”1”

x==y 返回true。

很显然每次new一个Name对象他们的引用值是不同的。

 

第二个Test.java

重写了Object类的equals()和hashCode()。

 

c.add(new Name("yang","xiao"))、c.remove(newName("yang","xiao")

两个Name("yang","xiao")是同一对象,因为我们重写了equals()和hashCode()。判断一个对象是否相等是根据我们自己定义的equals()和hashCode()方法。两个对象内容相等我们就认为他们是同一个对象。

 

关键:重写equals()和hashCode()。

到这里我想大家应该明白了吧。

这是我的处女之作,有什么不足地方希望大家可以提出来。谢谢。