Different reference stacks return equals true - java

towers=new ArrayList<>();
towers.add(new Stack<Integer>());
towers.add(new Stack<Integer>());
towers.add(new Stack<Integer>());
Why would this return true?
towers.get(1).equals(towers.get(2))
Did they somehow implement equals for stacks to check elements instead of the default behaviour which is reference

Because of the equals contract for stacks.
http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Follow that to:
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#equals(java.lang.Object)
To wit:
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.

Related

How to comapre two list of hibernate entities are equal

I have list of 2 hibernate entities.
I need to compare them. When I use equals() it always gives false as its reference is different.
E1.equals(E2)
It's because you're taking a reference to the List object only when evaluating equality. You would have to iterate through the collections to check (assuming the ordering is important):
E1.size() == E2.size() && IntStream.range(0, E1.size())
.allMatch(index -> E1.get(index).equals(E2.get(index)));
P.S.1: Variable names are written lowercase in java.
P.S.2: Make sure you're defining equals and hashCode properly. By default same entity is not equal in different states (managed, detached).

Validate two lists together using Stream

Wish to compare, two (object) lists for
Not null
Not empty
Equal Size
Nth Element Field values are same
Possible?
String A = "one,two,three|four,five,six|seven,eight,nine"
String B = "three,six,nine"
List L1 = List.of(A.split("\\|"));
List L2 = List.of(B.split(","));
Give object of List L1, if the third sub value of an element matches with element of the List L2.
Note: This answered the question when it was:
Wish to compare, two (object) lists for
1. Not null
2. Not empty
3. Equal Size
4. Nth Element Field values are same
Possible?
Since then, it was significantly changed...
Seems like you can go with Objects.equals(list1, list2);
When only one of the lists is null, it returns false.
2./3. When the sizes are different, it will return false.
When the elements differ, it will also return false.
In any other case, it will return true.
Disclaimer: This works for the standard Lists in the Collections Framework. There might be other implementations which implement equals() differently (and therefore behave differently when applied to Objects.equals()).

How to assert that lists are equal with testng?

I found an answer for junit, but need a solution for testng. Any ideas more usefull as writing an own for loop?
There's no need for a separate method for List comparison. Two lists can be compared by org.testng.Assert#assertEquals(Object, Object).
If two lists a and b are non-null, the call Assert.assertEquals(a, b) means a.equals(b) will be called subsequently.
And java.util.List#equals is what you need, as described in javadoc:
Compares the specified object with this list 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. This
definition ensures that the equals method works properly across
different implementations of the List interface.

difference between a.equals(b) and array.equals(a,b) [duplicate]

This question already has answers here:
equals vs Arrays.equals in Java
(9 answers)
Closed 7 years ago.
I have run this small program:
String[] a = {"a","b"};
String[] b = {"a","b"};
if (a.equals(b)){
System.out.println("woop");
}else{
System.out.println("doh!");
}
if (Arrays.equals(a, b)){
System.out.println("woop");
}else{
System.out.println("doh!");
}
The output of the program is "doh! woop".
I get the difference between the == and .equals but what is the difference between these equals operations?
TL;DR
equals compares array instances, Arrays.equals compares array elements.
Full Explanation
Arrays do not implement equals. As a result, they hand the method off to Object which is implemented as follows.
public boolean equals(Object obj) {
return (this == obj);
}
Thus, equals on an array is the same as ==.
Arrays.equals has the following description. It compares the elements of the array.
Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
Arrays.equals(Object[] a, Object[] a2)...
returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
In short, all your Strings are compared through equals.
See API.
Object.equals, which is invoked when comparing two arrays with the array1.equals(array2) idiom, compares the reference, just like ==.
See source for java.lang.Object:
public boolean equals (Object o) {
return this == o;
}
The difference is that a.equals(b) will use == under the hood, since arrays don't override the Object.equals() method. You use this to test for instance equality between arrays.
So in your case, a.equals(b) will return false, since the two arrays are different instances.
Instead, Arrays.equals() actually compares what's inside the arrays. If the arrays contain the same values, then Arrays.equals() returns true. You use this to test for semantic equality between arrays.
With arrays, a.equals(b) is the same as a == b. This is checking if the two array instances are the same. Arrays.equals(a,b) takes the time to compare each element of both arrays and checks if the elements are equal, which is different than checking if the containers themselves are equal.
A quick analogy: Let's say a and b are buckets. Asking if the two buckets are the same is not the same as asking if what is in the buckets is the same.

The **equals** in Set interface

As we know two Set instances are equal iff they contain the same elements, BUT is it possible to have the same element in two different sets (Set interface can not contain duplicate element)?
Distinct sets have no affect on one another. Set A can contain '123456', and Set B can also contain '123456' - the prohibition on duplicates is for a single instance, not across instances.
Equality for two sets implies that their contents are identical.
You are not comparing the elements inside a Set with each other to detect if they are equal, you are comparing the Elements of Set1 with the elements of Set2.
the equals method is described pretty well here.

Categories

Resources