LinkedList底层源码是采用双向链表实现的

 private static class Node<E> {
E item;//节点值
Node<E> next;//节点后指针
Node<E> prev;//节点前指针 Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}

何为双向列表,单向链表为通过后继可以找到下一个指向的元素;双向链表为既可以通过后继找到下一个指向的元素,也可以通过前驱找到前一个元素。基于链表实现的方式使得LinkedList在插入和删除时更优于ArrayList,而随机访问则比ArrayList逊色些

HashSet

/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}


源码中可以看到,hashSet的实现实际是由hashMap实现的,hashMap默认长度是16

HashSet中的add方法源码

 /**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

是通过map.put 其中对象e作为key,而

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

PERSENT作为value ,所以hashSet是唯一的,因为HashMap中key是唯一的

HashMap

  hashMap是基于数组和单链表实现的

static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash; /**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
} public final K getKey() {
return key;
} public final V getValue() {
return value;
} public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
} public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
} public final int hashCode() {
return (key==null ? 0 : key.hashCode()) ^ (value==null ? 0 : value.hashCode());
} public final String toString() {
return getKey() + "=" + getValue();
} /**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
} /**
* This method is invoked whenever the entry is
* removed from the table.
*/
void recordRemoval(HashMap<K,V> m) {
}
}

(1)HashMap的内部存储结构其实是数组和链表的结合。当实例化一个HashMap时,系统会创建一个长度为Capacity的Entry数组,这个长度被称为容量(Capacity),在这个数组中可以存放元素的位置我们称之为“桶”(bucket),每个bucket都有自己的索引,系统可以根据索引快速的查找bucket中的元素。每个bucket中存储一个元素,即一个Entry对象,但每一个Entry对象可以带一个引用变量,用于指向下一个元素,因此,在一个桶中,就有可能生成一个Entry链。 
  (2) 在存储一对值时(Key—->Value对),实际上是存储在一个Entry的对象e中,程序通过key计算出Entry对象的存储位置。换句话说,Key—->Value的对应关系是通过key—-Entry—-value这个过程实现的,所以就有我们表面上知道的key存在哪里,value就存在哪里。

转自:https://www.cnblogs.com/liang1101/p/6398593.html

LinkedList,HashSet,HashMap的更多相关文章

  1. [Java语言] HashMap,HashSet,Hashtable,Vector,ArrayList 的关系 <转>

    这么几个比较常用的但是比较容易混淆的概念同出于 java.util 包.本文仅作几个类的浅度解析. (本文基于JDK1.7,源码来自openjdk1.7.) ├── Collection │ ├── ...

  2. 09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHashSet,TreeSet

    09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHas ...

  3. 对hashmap,hashset,hashtable的理解

    1.首先先理一下java的集合关系,Collection和Map接口是所有集合接口的根结点,其他集合都直接或者间接的实现了他们中的一个:collection下有:list(元素可重复)和set(不可重 ...

  4. 一、集合框架(关于ArrayList,LinkedList,HashSet,LinkedHashSet,TreeSet)

    一.ArrayList 解决了数组的局限性,最常见的容器类,ArrayList容器的容量capacity会随着对象的增加,自动增长.不会出现数组边界的问题. package collection;   ...

  5. == 和 equals,equals 与 hashcode,HashSet 和 HashMap,HashMap 和 Hashtable

    一:== 和 equals == 比较引用的地址equals 比较引用的内容 (Object 类本身除外) String obj1 = new String("xyz"); Str ...

  6. JDK类集框架实验(ArrayList,LinkedList,TreeSet,HashSet,TreeMap,HashMap)

        ArrayList import java.util.ArrayList; public class C8_3 { public static void main(String[] args) ...

  7. (9)集合之Set,HashSet,TreeSet

    TreeSet子类 注意事项: 1.向TreeSet添加元素的时候,如果元素本身具备了自然顺序的特性,那么就按照元素自然顺序的特性进行排序存储 2.往TreeSet添加元素的时候,如果元素本身不具备自 ...

  8. Java 学习路线以及各阶段学习书籍,博文,视频的分享

    感谢: 感谢每一个打开这篇文章的人,听我在这里瞎扯!至于我为什么会有闲情写这篇文章呢?因为我每天想的是为什么要给我这样的需求,背后的人性是什么,我能再做些什么能让他更好.久而久之,我也稍微有了些自己的 ...

  9. 【转】Parcelable, Serializable,Cloneable,copyProperties

    Copying ... https://blog.csdn.net/max2005/article/details/78325036 存在着三件事,整理如下 Parcelable, Serializa ...

随机推荐

  1. 2734: [HNOI2012]集合选数

    2734: [HNOI2012]集合选数 链接 分析: 转化一下题意. 1 3 9 27... 2 6 18 54... 4 12 36 108... 8 24 72 216... ... 写成这样的 ...

  2. 4513: [Sdoi2016]储能表

    4513: [Sdoi2016]储能表 链接 分析: 数位dp. 横坐标和纵坐标一起数位dp,分别记录当前横纵坐标中这一位是否受n或m的限制,在记录一维表示当前是否已经大于k了. 然后需要两个数组记录 ...

  3. sublime常用方法

    1.如何打开一个文件夹? project----->Add Folder to Project 2.如何同一个窗口下进行分屏操作? 使用快捷键:shift+Alt+2 3.如何使html代码进行 ...

  4. springmvc框架开发常用的注解总结

    1.@Controller使用:表示表现层中的JavaBean被spring容器管理.   2.@requestMapping使用: a) 在方法上: 标记url到请求方法的映射, 就相当于从一个ur ...

  5. 16节实用性爆棚的Ps课:零基础秒上手,让你省钱也赚钱

    ps视频教程,ps自学视频教程.ps免费视频教程下载,16节实用性爆棚的Ps课教程视频内容较大,分为俩部分: 16节实用性爆棚的Ps课第一部分:百度网盘,https://pan.baidu.com/s ...

  6. Fiddler接口测试(一)post接口测试

    项目背景: 1.接口URL:http://192.168.xx.xx:8080/mserver/rest/ms 2.接口参数:data=xxxxx&key=xxxxx,数据是加密的 另一种参数 ...

  7. 曲线救国:安装golang.org/x/*

    很多第三方库都用到了golang.org/x下面的包,但是国内不通过VPN是无法访问到的,不过没关系,在github.com/golang下面,基本都有对应的包 如何操作? 以安装sys包为例 git ...

  8. 我的第一个bootstrap实例

    先上代码: <!doctype html><html lang="en"><head> <meta charset="UTF-8 ...

  9. PHP核心技术——继承与多态

    继承: class person{ public $name='Tom'; public $gender; static $money=10000; public function __constru ...

  10. python基础知识-12-模块的了解

    python其他知识目录 1.模块介绍: Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句.模块让你能够有逻辑地组织你 ...