首页 > 编程语言 > 详细

How to sort HashSet in Java

时间:2019-08-24 16:04:35      阅读:80      评论:0      收藏:0      [点我收藏+]

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]

How to sort HashSet in Java

原文:https://www.cnblogs.com/hglibin/p/11404292.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!