Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .equals on Hashset return true regardless of order?

Tags:

java

hashset

For the Hashset in java there is a .equals method comparing elements in each set. Will this return true regardless of order?

Example, let's say we have one set with elements {a,b,c} and another with elements {b,c,a}

if you use .equals on these two sets will it return true, or does it have to be sorted?

like image 914
user1054740 Avatar asked Jan 28 '13 04:01

user1054740


People also ask

Does HashSet guarantee order?

It means that HashSet does not maintains the order of its elements. Hence sorting of HashSet is not possible. However, the elements of the HashSet can be sorted indirectly by converting into List or TreeSet, but this will keep the elements in the target type instead of HashSet type.

Does HashSet follow order?

HashSet does not maintain any order while LinkedHashSet maintains insertion order of elements much like List interface and TreeSet maintains sorting order or elements.

Does order matter in sets Java?

The Set interface itself does not stipulate any particular order.

Does HashSet use equals or compareTo?

Comparison method HashSet uses equal() and hashcode() methods to compare the elements, while TreeSet we can implements compareTo() method of Comparator interface so we have compare() and compareTo() method ,TreeSet does not use equal() and hashcode() method.


5 Answers

This should return true. The documentation says :

Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

like image 56
Deepak Borania Avatar answered Oct 05 '22 23:10

Deepak Borania


HashSet has no order

Java HashSets are unordered – they do not have an ordering. Therefore your question simply cannot be asked as posed. The set {a,b,c} is the same as the set {b,c,a}. That said, HashSet inherit's AbstractSet#equals(Object) which tells us the following:

Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

like image 42
Matt Ball Avatar answered Oct 06 '22 01:10

Matt Ball


HashSet implements the Set interface(modeling the mathematical set abstraction), which contains no order information and therefore sort makes no sense in a set. As a result, equals in a Set only considers the members and disregards the orders of members.

like image 40
Hui Zheng Avatar answered Oct 05 '22 23:10

Hui Zheng


Yes, it will be true because a Set has no implicit order.

You can apply order to a set by using adding a Comparator, or using a special case of Set, such as TreeSet.

Incidentally, a hashcode is used for fast comparison in a Set. According to the equals contract, any two objects that are considered "equal" must have the same hashcode.

This means a Set only has to fall-back to more time consuming methods in the unlikely event of a hashcode colission.

like image 27
Jasper Blues Avatar answered Oct 05 '22 23:10

Jasper Blues


Yes, a HashSet is an unordered list of unique elements, meaning the order of the elements has no impact on equality.

Below is a demonstration illustrating this.

import java.util.HashSet;

public class TestHashSet 
{
    public static void main(String[] args) 
    {
        HashSet<Integer> set1 = new HashSet<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
        }};
        HashSet<Integer> set2 = new HashSet<Integer>() {{
            add(3);
            add(1);
            add(5);
            add(4);
            add(2);
        }};
        System.out.println("set1 is equal to set2: " + set1.equals(set2));
    }
}
like image 44
Dmitry Avatar answered Oct 06 '22 01:10

Dmitry