e.HashSet与LinkedHashSet源码解析(1.7)
来源:互联网 发布:数据库管理系统产品 编辑:程序博客网 时间:2024/05/20 09:08
java容器类分为两大类,一部分是Map,用来存放键值对。另一部分是Collection,用来存放独立的元素。Collection分为List与Set,List可以有重复顺序,Set则不行
我们要分析的HashSet与LinkedHashSet底层都是用的HashMap与LinkedHashMap的key来实现的,这里将具体分析下具体的封装
<pre name="code" class="java">public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
属性:
<span style="white-space:pre"></span>private transient HashMap<E,Object> map;//没错,就是用它的key来存储元素的 <span style="white-space:pre"></span>private static final Object PRESENT = new Object();//常量,它的作用基本可以看出来,是用来当做某种初始值,如果往下看的话应该明白,就是作为Map的Value
构造方法:
//看到map=new HashMap(),往下就是一切尽在不言中了,我们只要看看set与map的联系即可
public HashSet() { map = new HashMap<>(); } public HashSet(Collection<? extends E> c) { map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); addAll(c); } public HashSet(int initialCapacity, float loadFactor) { map = new HashMap<>(initialCapacity, loadFactor); } public HashSet(int initialCapacity) { map = new HashMap<>(initialCapacity); } //注意,这个将会在LinkedHashSet里头用到 HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<>(initialCapacity, loadFactor); }
方法:
迭代器:这里赤裸裸的直接用上了keySet.iterator();
public Iterator<E> iterator() { return map.keySet().iterator(); }
size与isEmpty一样,也是直接调用了map对象的方法
public int size() { return map.size(); } public boolean isEmpty() { return map.isEmpty(); }
往下一个套路,就不再多说了
***********************************************************************************
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, java.io.Serializable类声明与HashSet大同小异,LinkedHashSet是HashSet的子类
他的构造方法通过调用HashSet的第五个构造方法,创建一个lingkedHashHap,其余与HashSet相同
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, java.io.Serializable { private static final long serialVersionUID = -2851667679971038690L; /** * Constructs a new, empty linked hash set with the specified initial * capacity and load factor. * * @param initialCapacity the initial capacity of the linked hash set * @param loadFactor the load factor of the linked hash set * @throws IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive */ public LinkedHashSet(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor, true); } /** * Constructs a new, empty linked hash set with the specified initial * capacity and the default load factor (0.75). * * @param initialCapacity the initial capacity of the LinkedHashSet * @throws IllegalArgumentException if the initial capacity is less * than zero */ public LinkedHashSet(int initialCapacity) { super(initialCapacity, .75f, true); } /** * Constructs a new, empty linked hash set with the default initial * capacity (16) and load factor (0.75). */ public LinkedHashSet() { super(16, .75f, true); } /** * Constructs a new linked hash set with the same elements as the * specified collection. The linked hash set is created with an initial * capacity sufficient to hold the elements in the specified collection * and the default load factor (0.75). * * @param c the collection whose elements are to be placed into * this set * @throws NullPointerException if the specified collection is null */ public LinkedHashSet(Collection<? extends E> c) { super(Math.max(2*c.size(), 11), .75f, true); addAll(c); }}
0 0
- e.HashSet与LinkedHashSet源码解析(1.7)
- 【Java源码】HashSet、LinkedHashSet
- 源码分析-HashSet、LinkedHashSet
- Java集合框架源码解析-HashSet及LinkedHashSet
- HashSet与LinkedHashSet
- HashSet与LinkedHashSet的区别
- Java HashMap(LinkedHashMap)与Hashset(LinkedHashSet)的排序
- LinkedHashSet类源码解析
- java源码分析之HashSet及LinkedHashSet
- HashSet及LinkedHashSet源码分析(…
- java 源码分析之HashSet及LinkedHashSet
- java源码分析之HashSet及LinkedHashSet
- java源码分析之HashSet和LinkedHashSet
- HashSet及LinkedHashSet之源码分析
- Java集合之HashSet,LinkedHashSet源码分析
- Java容器HashSet和LinkedHashSet源代码解析
- HashSet-LinkedHashSet
- Set接口中的HashSet与LinkedHashSet
- Beautiful Year
- 像360悬浮窗那样,用WindowManager做一个炫酷的悬浮迷你音乐盒(上)
- (字典)rot13加密
- React Native BUG(001)
- Android Dialog、Toast和Snackbar详解
- e.HashSet与LinkedHashSet源码解析(1.7)
- 感知机中损失函数1/||w||为什么可以不考虑(或直接忽略)?
- 【利器】Vim配置成IDE方案以及使用
- 517.Ugly Number-丑数(容易题)
- 使用SourceTree提交代码到github
- Linux网络中通知链的使用
- 第四周 项目2-建设“单链表”算法库
- 一句代码读取properties文件内容
- listview嵌套gridview 条目里面gridview显示不全的问题