I'm trying to create a HashSet (or any collection type - but I think HashSet will suit me best) that will remain in order no matter what is inserted. It's for a contact manager project I am working on. I've been experimenting, with the example below.
import java.util.*;
public class TestDriver{
public static void main(String[] args)
{
FullName person1 = new FullName("Stephen", "Harper");
FullName person2 = new FullName("Jason", "Kenney");
FullName person3 = new FullName("Peter", "MacKay");
FullName person4 = new FullName("Rona", "Ambrose");
FullName person5 = new FullName("Rona", "Aabrose");
HashSet<FullName> names = new HashSet<FullName>();
names.add(person3);
names.add(person1);
names.add(person4);
names.add(person2);
System.out.println(names);
}
}
I expected the output to put the names in alphabetical order - at least according to either their first or last name. However, I can't even discern the method HashSet used to come up with this ordering;
[Jason Kenney, Rona Ambrose, Stephen Harper, Peter MacKay]
My question is, how do I tell my program how to sort the names based on my specifications?
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.
We can not predict the insertion order in HashSet, but we can predict it in LinkedHashSet. The LinkedHashSet extends the HashSet, so it uses a hashtable to store the elements. Moreover, it uses a doubly linked list to maintain the insertion order.
A HashSet is an unordered collection.
A set is used to provide a particular ordering on its element. The elements are ordered either by using a natural ordering or by using a Comparator. All the elements which are inserted into a sorted set must implement the Comparable interface. The set's iterator will traverse the set in an ascending order.
HashSet does not provide any meaningful order to the entries. The documentation says:
It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
To get a sensible ordering, you need to use a different Set implementation such as TreeSet or ConcurrentSkipListSet. These implementations of the SortedSet interface let you provide a Comparator that specifies how to order the entries; something like:
public class SortByLastName implements Comparator<FullName>{
public int compare(FullName n1, FullName n2) {
return n1.getLastName().compareTo(n2.getLastName());
}
}
TreeSet<FullName> names = new TreeSet<FullName>(new SortByLastName());
You could instead make the FullName class implement the Comparable interface, but this might be unhelpful if you wanted to sometimes sort by last name, sometimes by first name, or other criteria.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With