How to sort HashSet in Java

方法一:By Converting HashSet to List

方法二:By Converting HashSet to TreeSet

import java.util.*;

public class HashSortOfSort {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("geeks");
        set.add("practice");
        set.add("contribute");
        set.add("ide");
        System.out.println(set);
        List<String> list1 = new ArrayList<>(set);
        Collections.sort(list1);
        System.out.println(list1);

        SortedSet<String> sortedSet = new TreeSet<>(set);
        System.out.println(sortedSet);
    }
}

[practice, geeks, contribute, ide]
[contribute, geeks, ide, practice]
[contribute, geeks, ide, practice]

相关文章:

  • 2021-07-16
  • 2022-02-26
  • 2021-05-20
  • 2022-02-20
  • 2021-07-29
  • 2021-09-10
  • 2021-09-24
  • 2022-03-02
猜你喜欢
  • 2021-11-04
  • 2022-01-30
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2021-07-12
相关资源
相似解决方案