java集合源码分析

List,Set,Map都是接口,前两个继承Collection接口,Map为独立接口
Set的实现由HashSet,LinkedHashSet,TreeSet
List下有ArrayList,Vector,LinkedList
Map下有Hashtable,LinkedHashMap,HashMap,TreeMap
Collection还有Queue接口,实现有PriorityQueue

ArrayList、LinkedList、HashMap中都有字段叫modCount,字段用途:
/**

  • The number of times this list has been <i>structurally modified</i>.
  • Structural modifications are those that change the size of the
  • list, or otherwise perturb it in such a fashion that iterations in
  • progress may yield incorrect results.
  • <p>This field is used by the iterator and list iterator implementation
  • returned by the {@code iterator} and {@code listIterator} methods.
  • If the value of this field changes unexpectedly, the iterator (or list
  • iterator) will throw a {@code ConcurrentModificationException} in
  • response to the {@code next}, {@code remove}, {@code previous},
  • {@code set} or {@code add} operations. This provides
  • <i>fail-fast</i> behavior, rather than non-deterministic behavior in
  • the face of concurrent modification during iteration.
  • <p><b>Use of this field by subclasses is optional.</b> If a subclass
  • wishes to provide fail-fast iterators (and list iterators), then it
  • merely has to increment this field in its {@code add(int, E)} and
  • {@code remove(int)} methods (and any other methods that it overrides
  • that result in structural modifications to the list). A single call to
  • {@code add(int, E)} or {@code remove(int)} must add no more than
  • one to this field, or the iterators (and list iterators) will throw
  • bogus {@code ConcurrentModificationExceptions}. If an implementation
  • does not wish to provide fail-fast iterators, this field may be
  • ignored.
    */

*
此列表在结构上被修改的次数。
结构修改是指改变
列出,或者以这样一种方式干扰它
进度可能会产生不正确的结果。

<p>此字段由迭代器和列表迭代器实现使用
由@code迭代器和@code lis迭代器方法返回。
如果此字段的值意外更改,则迭代器(或列表
迭代器)将在
响应@code next,@code remove,@code previous,
@code set或@code add操作。这提供了
<i>fail fast</i>behavior,than non determinatic behavior in
迭代期间并发修改的面。

<p><b>按子类使用此字段是可选的。<b>如果是子类
希望提供fail-fast迭代器(和list迭代器),然后
只需在其@code add(int,e)中增加该字段,
@code remove(int)方法(以及它重写的任何其他方法)
这将导致对列表进行结构修改)。打个电话给
@code add(int,e)或@code remove(int)必须添加不超过
一个到这个字段,或者迭代器(和列表迭代器)将抛出
伪造{@code ConcurrentModificationExceptions}。如果一个实现
不希望提供fail-fast迭代器,此字段可能是
已忽略。
/

List<String> list=new ArrayList();
list.add("config");
list.add("config");
list.add("config1");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.add("config");
list.forEach(s -> {
if("config1".equals(s)){
list.remove(s);
}
});

java.util.ConcurrentModificationException
at java.util.ArrayList.forEach(ArrayList.java:1260)
at com.mufeng.test.base.dataStructure.TestList.test1(TestList.java:31)

//HashSet
//巧妙利用HashMap中key实现

private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
// 仿真的值与Map中对象保持一致
private static final Object PRESENT = new Object();
public HashSet() {
map = new HashMap<>();
}

public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}

//LinkedHashSet
//继承HashSet
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {

//初始容量为16
public LinkedHashSet() {
super(16, .75f, true);
}

//LinkedHashMap
//继承HashMap 好多方法都可以用HashMap中的
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>

static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}

/**
 * The head (eldest) of the doubly linked list.
 */
 //单链表 首位
transient LinkedHashMap.Entry<K,V> head;

/**
 * The tail (youngest) of the doubly linked list.
 */
 //末位
transient LinkedHashMap.Entry<K,V> tail;

/**
 * The iteration ordering method for this linked hash map: <tt>true</tt>
 * for access-order, <tt>false</tt> for insertion-order.
 *
 * @serial
 */
final boolean accessOrder;

//TreeSet
//具体实现为TreeMap
private transient NavigableMap<E,Object> m;

// Dummy value to associate with an Object in the backing Map
//仿真值
private static final Object PRESENT = new Object();

public TreeSet() {
    this(new TreeMap<E,Object>());
}
//利用TreeMap的key
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}

public boolean remove(Object o) {
    return m.remove(o)==PRESENT;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值