简体   繁体   中英

Telling HashSet how to sort the data

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?

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.

use Treeset for natural ordering.

HashSet--- not ordered/sorted
LinkedhashSet--- maintains insertion order
TreeSet--- sorts in natural order

for your case use TreeSet instead.

HashSet doesn't preserve order, Go for TreeSet and implement your own Comparator to instruct TreeSet how to compare

new TreeSet<FullName>(new Comparator<FullName>(){
        public int compare(Fullname one, FullName two{/*logic*/}
});

See

Seems like you need TreeSet to achieve alphabetical order or LinkedHashSet to preserve insertion order.

Note that your FullName must implement Comparable<FullName> in order to be used in TreeSet (or you have to provide external Comparator`).

尝试这个:

 System.out.println(names.toList.sorted)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM