集合

框架关系图:

Collection接口下面有三个子接口:List、Set、Queue。此篇是关于Set<E>的简单学习总结。

补充:HashTable父类是Dictionary,不是AbstractMap。

Set:

Set里存放的对象是无序,不能重复的,集合中的对象不按特定的方式排序,只是简单地把对象加入集合中(可以存空元素)。常见的子类:HashSet、TreeSet、LinkedHashSet。

1、HashSet(无序,不可重复):

底层实际上是一个无序,不可重复的HashMap,源代码如下:

     private transient HashMap<E,Object> map;
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*(默认初始容量是16,加载因子是0.75,元素个数超过16*0.75就扩容)
*/
public HashSet() {
map = new HashMap<>();
}
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}

因为Set的无序,不可重复,所以常用来去重。

     public static void main(String[] args) {
Set<String> hash = new HashSet<String>();
hash.add("C");
hash.add("A");
hash.add("A");
hash.add("D");
hash.add("B");
hash.add("B");
hash.add("D");
hash.add("C");
Iterator it = hash.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}

注意:当HashSet调用add(Object o)时,会把参数当为key,默认一个对象最为value:

     private static final Object PRESENT = new Object();
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

2、LinkedHashSet(有序,不可重复):

LinkedHashSet继承HashSet,默认初始容量:16,加载因子:0.75。其实LinkedHashSet就是双向链状的HashSet,因为是链状,所以实现了有序性,但是不可重复。

     public static void main(String[] args) {
Set<String> linked = new LinkedHashSet<String>();
linked.add("A");
linked.add("A");
linked.add("C");
linked.add("B");
linked.add("D");
linked.add("A");
linked.add("B");
Iterator it = linked.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}

3、TreeSet(有序,不可重复):

TreeSet底层是TreeMap:

 public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable{

     private transient NavigableMap<E,Object> m;

     private static final Object PRESENT = new Object();

     TreeSet(NavigableMap<E,Object> m) {
this.m = m;
} public TreeSet() {
this(new TreeMap<E,Object>());
}

TreeSet中有个Compare比较器,会对数据进行排序:

     public static void main(String[] args) {
Set<String> tree = new TreeSet<>();
tree.add("A");
tree.add("C");
tree.add("A");
tree.add("D");
tree.add("B");
Iterator<String> it = tree.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}

最新文章

  1. 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument
  2. mvc 导出excel
  3. cocos2dx Sprite setBlendFunc 使用颜色混合:加算,减算
  4. C# 中的装箱与拆箱
  5. 自定义函数实现NULL值替换
  6. WCF编程系列(二)了解WCF
  7. WSGI是一种编程接口,而uwsgi是一种传输协议
  8. 【UVALive - 5131】Chips Challenge(上下界循环费用流)
  9. WordPress插件制作教程(七): 插件函数之过滤器(Filter)函数
  10. POJ 2019 Cornfields(二维RMQ)
  11. 调皮的QQ音乐API:修复无法获取歌单
  12. 【十八】php文件下载源码
  13. mysql中使用enum,如何获取所有可能的值
  14. uva 10918 - Tri Tiling(规律)
  15. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏
  16. mutiplemap 总结
  17. 170725、Kafka原理与技术
  18. js去重复和取重复数据
  19. jQuary总结4: jquery操作字符串
  20. Java 语言基础之数组(一)

热门文章

  1. nginx配置文件的通用语法介绍
  2. js笔记(5)--location的用法
  3. Codeforces_714
  4. Codeforces_713_A
  5. spring中获取bean的方式
  6. 2.5D(伪3D)站点可视化第一弹
  7. pthread_cond_broadcast &amp; pthread_cond_signal
  8. Gdal随笔
  9. CentOS 7 GNOME桌面系统 网络配置
  10. ELF文件之一——