How to sort HashSet in Java
发布时间:2020-12-15 07:57:49 所属栏目:Java 来源:网络整理
导读: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) { SetString set = new HashSet(); set.add("
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,ide,practice] [contribute,practice] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |