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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…