HashSet /LinkedHashSet/TreeSet排序的不同

来源:互联网 发布:淘宝网鱼缸刷 编辑:程序博客网 时间:2024/04/30 04:40
 
  1. HashSet set1 = new HashSet(Arrays.asList("b c e f g a".split(" ")));  
  2.         LinkedHashSet set2 = new LinkedHashSet(Arrays.asList("b c e f g a".split(" ")));  
  3.         SortedSet set3 = new TreeSet(Arrays.asList("b c e f g a".split(" ")));  
  4.         System.out.println(set1);  
  5.         System.out.println(set2);  
  6.         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按字母顺序排序

/**

 *    description 几个set的比较
 *    HashSet:哈希表是通过使用称为散列法的机制来存储信息的,元素并没有以某种特定顺序来存放;
 *    LinkedHashSet:以元素插入的顺序来维护集合的链接表,允许以插入的顺序在集合中迭代;
 *    TreeSet:提供一个使用树结构存储Set接口的实现,对象以升序顺序存储,访问和遍历的时间很快。
 */