HashSet set1 = new HashSet(Arrays.asList("b c e f g a".split(" ")));
LinkedHashSet set2 = new LinkedHashSet(Arrays.asList("b c e f g a".split(" ")));
SortedSet set3 = new TreeSet(Arrays.asList("b c e f g a".split(" ")));
System.out.println(set1);
System.out.println(set2);
System.out.println(set3);
输出结果为:
[f, g, e, b, c, a]
[b, c, e, f, g, a]
[a, b, c, e, f, g]
HashSet按Hash函数排序
LinkedHashSet按插入顺序排序
TreeSet按字母顺序排序
本文深入探讨了三种集合类在Java中排序的区别:HashSet按哈希函数排序,LinkedHashSet按插入顺序排序,而TreeSet则按字母顺序排序。通过实例演示,清晰展示了每种集合类的特点和应用场景。
2192

被折叠的 条评论
为什么被折叠?



