java集合的数据结构_java集合数据结构汇总

一、Set

HashSet其实是用了HashMap的数据结构。HashMap的数据结构是一个数组+链表的形式。

关于HashMap数据结构的具体实现、以及扩容,参考博客:https://www.jianshu.com/p/8b372f3a195d/

TreeSet用了TreeMap的数据结构,TreeMap的本质是一个红黑树,TreeMap类里存着根节点:

private transient Entryroot;

二、List

ArrayList内部是数组:

transient Object[] elementData;

public boolean add(E e) {

ensureCapacityInternal(size + 1); // Increments modCount!!

elementData[size++] = e;

return true;

}

LinkedList内部是链表:

transient Nodefirst;

/**

* Pointer to last node.

* Invariant: (first == null && last == null) ||

* (last.next == null && last.item != null)

*/

transient Nodelast;

public boolean add(E e) {

linkLast(e);

return true;

}

void linkLast(E e) {

final Nodel = last;

final NodenewNode = new Node<>(l, e, null);

last = newNode;

if (l == null)

first = newNode;

else

l.next = newNode;

size++;

modCount++;

}

linkLast在链表结尾处添加节点,改变尾节点的next指针指向新节点。

三、Map

HashMap的数据结构是一个数组+链表的形式。

TreeMap的本质是一个红黑树。

LinkedHashMap的数据结构是HashMap+链表,既用HashMap的数据结构存储,又给每个节点加上before和after维护插入顺序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值