开发者

Java List , Vector comparison

I saw the following program on the internet

public class开发者_JS百科 Test1{
 public static void main(String[] args)
 {
 Integer int1 = new Integer(10);
 Vector vec1 = new Vector();
 LinkedList list = new LinkedList();
 vec1.add(int1);
 list.add(int1);
 if(vec1.equals(list)) System.out.println("equal");
 else  System.out.println("not equal");
  }

}

The answer it print is "equal".

How it is possible?

Thanks

Dilip


Because both a LinkedList and Vector implement List#equals()

Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Specifically, here is the exact reason for this behavior.

This definition ensures that the equals method works properly across different implementations of the List interface.


Here's the API doc for Vector.equals() (other List implementations behave similarly):

Compares the specified Object with this Vector for equality. Returns true if and only if the specified Object is also a List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal.


See the javadoc for equals on java.util.Vector:

  /**
     * Compares the specified Object with this Vector for equality.  Returns
     * true if and only if the specified Object is also a List, both Lists
     * have the same size, and all corresponding pairs of elements in the two
     * Lists are <em>equal</em>.  (Two elements {@code e1} and
     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
     * e1.equals(e2))}.)  In other words, two Lists are defined to be
     * equal if they contain the same elements in the same order.
     *
     * @param o the Object to be compared for equality with this Vector
     * @return true if the specified Object is equal to this Vector
     */

The equals method is checking that the lists have the same contents in the same order.


http://download.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html#equals%28java.lang.Object%29

Definition of equals implementation on Vector:

Compares the specified Object with this Vector for equality. Returns true if and only if the specified Object is also a List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two Lists are defined to be equal if they contain the same elements in the same order.

Both Vector and LinkedList are descendents of AbstractList (in the case of LinkedList, there's an extra class in between I believe). AbstractList objects can be compared to one another.


It is because they are both lists.

Vector and LinkedList both inherit ultimately from AbstractList.

See the implementation of equals on line 512 of the source code.


That's because both LinkedList and Vector internally call the same method java.util.AbstractList.equals() to do the comparison.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜