摘要: 1、数据结构相关的类,如String、ArrayList,LinkedList,HashMap和ConcurrentHashMap等等。2、线程并发相关的类,如Synchronized、ReentrantLock、ReadWriteLock、AQS和线程池的实现等。 阅读全文
posted @ 2018-04-18 16:32 Entropy_lxl 阅读(598) 评论(0) 推荐(0) 编辑

ArrayList (数组链表)使用Object数组作为存储。

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer. Any
 * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
 * DEFAULT_CAPACITY when the first element is added.
 */
private transient Object[] elementData;
 
/**
 * Shared empty array instance used for empty instances.
 */
private static final Object[] EMPTY_ELEMENTDATA = {};

  

 默认构造函数为:

1
2
3
4
5
6
7
/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    super();
    this.elementData = EMPTY_ELEMENTDATA;
}

 构造一个空的链表,该链表没有进行初始化,在调用其add()方法时,进行了初始化:

1
2
3
4
5
6
7
8
9
10
11
/**
 * Appends the specified element to the end of this list.
 *
 * @param e element to be appended to this list
 * @return <tt>true</tt> (as specified by {@link Collection#add})
 */
public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

 ensureCapacityInternal()方法是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private void ensureCapacityInternal(int minCapacity) {
    if (elementData == EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }
 
    ensureExplicitCapacity(minCapacity);
}
 
private void ensureExplicitCapacity(int minCapacity) {
    modCount++;
 
    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
 
/**
 * Increases the capacity to ensure that it can hold at least the
 * number of elements specified by the minimum capacity argument.
 *
 * @param minCapacity the desired minimum capacity
 */
private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
}

  

get()方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Returns the element at the specified position in this list.
 *
 * @param  index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    rangeCheck(index);
 
    return elementData(index);
}
 
@SuppressWarnings("unchecked")
E elementData(int index) {
    return (E) elementData[index];
}

   

  

posted @ 2018-04-19 16:50 Entropy_lxl 阅读(262) 评论(0) 推荐(0) 编辑
摘要: 1. String是使用char[]数组来存储的,并且String值在创建之后就不可以改变了。char[]数组的定义为: char[]数组value使用final修饰,因此赋值之后就不可以改变了。再看一下String的hashCode()方法的实现就更能说明这一点: 成员变量hash,用来缓存Str 阅读全文
posted @ 2018-04-19 11:24 Entropy_lxl 阅读(829) 评论(0) 推荐(0) 编辑
摘要: ConcurrentHashMap (JDK 1.7)的继承关系如下: 1. ConcurrentHashMap是线程安全的hash map。ConcurrentHashMap的数据结构是一个Segment<K, V>数组: 其中,HashEntry的定义如下: 因此,Segment数组的意义就是将 阅读全文
posted @ 2018-04-12 17:52 Entropy_lxl 阅读(290) 评论(0) 推荐(0) 编辑
摘要: HashTable继承关系如下: HashTable是一个线程安全的【键-值对】存储结构。其存储结构和HashMap相同,参考这里。 1. HashTable定义了一个类型为Entry<K,V>的数组table用来存储数据。 类型Entry<K,V>的定义如下: 由Entry<K,V>的定义可知,上 阅读全文
posted @ 2018-04-12 16:51 Entropy_lxl 阅读(278) 评论(0) 推荐(0) 编辑
摘要: HashSet (jdk 1.7)的继承关系如下: HashSet是使用HashMap实现的一个没有重复元素的集合。HashSet用法如下: 从HashSet的add()方法可以看出,只有一个参数,并没有【键-值对】。 其实是HashSet只使用了HashMap的key,value统一是一个固定的O 阅读全文
posted @ 2018-04-12 15:41 Entropy_lxl 阅读(192) 评论(0) 推荐(0) 编辑
摘要: hashmap (jdk 1.7)使用 “数组-链表” 方式进行存储,图形化表示如下: 即,前面是一个数组,后面跟一个链表,那么数据结构这个对应到HashMap的代码里面是什么样子的呢? 在HashMap中定义了一个类型为Entry<K,V>的数组table,上图就是显示了这个table。 类型En 阅读全文
posted @ 2018-04-10 18:06 Entropy_lxl 阅读(290) 评论(0) 推荐(0) 编辑
摘要: 一、进程进程:指在系统中能独立运行并作为资源分配的基本单位,它是由一组机器指令、数据和堆栈等组成的,是一个能独立运行的活动实体。 注意,进程一般有三个状态:就绪状态、执行状态和等待状态【或称阻塞状态】;进程只能由父进程建立,系统中所有的进程形成一种进程树的层次体系;挂起命令可由进程自己和其他进程发出 阅读全文
posted @ 2018-03-25 11:01 Entropy_lxl 阅读(320) 评论(0) 推荐(0) 编辑
摘要: lsof 是什么意思? 答: list open files 查看某个文件被哪些进程在读写 lsof 文件名查看某个进程打开了哪些文件lsof –c 进程名lsof –p 进程号lsof用法小全 lsof +d /usr/local/ 显示目录下被进程开启的文件lsof +D /usr/local/ 阅读全文
posted @ 2018-03-06 10:28 Entropy_lxl 阅读(8319) 评论(0) 推荐(0) 编辑
摘要: 1. 使用ReentrantLock 结果为: 阅读全文
posted @ 2018-03-01 09:19 Entropy_lxl 阅读(202) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示