How to sort a HashSet?

后端 未结 19 2401
耶瑟儿~
耶瑟儿~ 2020-12-02 12:42

For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet?

相关标签:
19条回答
  • 2020-12-02 13:20
    1. Add all set element in list -> al.addAll(s);
    2. Sort all the elements in list using -> Collections.sort(al);
    
    
     public class SortSetProblem {
     public static void main(String[] args) {
        ArrayList<String> al = new ArrayList();
        Set<String> s = new HashSet<>();
        s.add("ved");
        s.add("prakash");
        s.add("sharma");
        s.add("apple");
        s.add("ved");
        s.add("banana");
        System.out.println("Before Sorting");
        for (String s1 : s) {
            System.out.print("  " + s1);
        }
    
        System.out.println("After Sorting");
        al.addAll(s);
        Collections.sort(al);
        for (String set : al) {
            System.out.print(" " + set);
        }
      }
     }
    

    input - ved prakash sharma apple ved banana

    Output - apple banana prakash sharma ved

    0 讨论(0)
  • 2020-12-02 13:20

    You can use guava library for the same

    Set<String> sortedSet = FluentIterable.from(myHashSet).toSortedSet(new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            // descending order of relevance
            //required code
        }
    });
    
    0 讨论(0)
  • 2020-12-02 13:21

    You can wrap it in a TreeSet like this:

    Set mySet = new HashSet();
    mySet.add(4);
    mySet.add(5);
    mySet.add(3);
    mySet.add(1);
    System.out.println("mySet items "+ mySet);   
    
    TreeSet treeSet = new TreeSet(mySet);   
    System.out.println("treeSet items "+ treeSet);   
    

    output :
    mySet items [1, 3, 4, 5]
    treeSet items [1, 3, 4, 5]

    Set mySet = new HashSet();
    mySet.add("five");
    mySet.add("elf");
    mySet.add("four");
    mySet.add("six");
    mySet.add("two");
    System.out.println("mySet items "+ mySet);
    
    TreeSet treeSet = new TreeSet(mySet);
    System.out.println("treeSet items "+ treeSet);
    

    output:
    mySet items [six, four, five, two, elf]
    treeSet items [elf, five, four, six, two]

    requirement for this method is that the objects of the set/list should be comparable (implement the Comparable interface)

    0 讨论(0)
  • 2020-12-02 13:22

    Elements in HashSet can't be sorted. Whenever you put elements into HashSet, it can mess up the ordering of the whole set. It is deliberately designed like that for performance. When you don't care about the order, HashSet will be the most efficient set for fast insertion and search.

    TreeSet will sort all the elements automatically every time you insert an element.

    Perhaps, what you are trying to do is to sort just once. In that case, TreeSet is not the best option because it needs to determine the placing of newly added elements all the time.

    The most efficient solution is to use ArrayList. Create a new list and add all the elements then sort it once. If you want to retain only unique elements (remove all duplicates like set does, then put the list into a LinkedHashSet, it will retain the order you have already sorted)

    List<Integer> list = new ArrayList<>();
    list.add(6);
    list.add(4);
    list.add(4);
    list.add(5);
    Collections.sort(list);
    Set<Integer> unique = new LinkedHashSet<>(list); // 4 5 6
    // The above line is not copying the objects! It only copies references.
    

    Now, you've gotten a sorted set if you want it in a list form then convert it into list.

    0 讨论(0)
  • 2020-12-02 13:24

    A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements.

    However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that:

    Set<?> yourHashSet = new HashSet<>();
    
    ...
    
    List<?> sortedList = new ArrayList<>(yourHashSet);
    Collections.sort(sortedList);
    
    0 讨论(0)
  • 2020-12-02 13:24

    You can use TreeSet as mentioned in other answers.

    Here's a little more elaboration on how to use it:

    TreeSet<String> ts = new TreeSet<String>();
    ts.add("b1");
    ts.add("b3");
    ts.add("b2");
    ts.add("a1");
    ts.add("a2");
    System.out.println(ts);
    for (String s: ts)
        System.out.println(s);
    

    Output:

    [a1, a2, a3, a4, a5]
    a1
    a2
    b1
    b2
    b3
    
    0 讨论(0)
提交回复
热议问题