List:

  ArrayList

    首先我们来看看jdk的ArrayList的add方法的源码是如何实现的:     

    public boolean add(E e) {
      ensureCapacityInternal(size + 1); // Increments modCount!!
      elementData[size++] = e;
      return true;
    }

    在AarryList类有如下数组的定义

/**
    * The array buffer into which the elements of the ArrayList are stored. --这是个存储ArrayList元素的数组
    * The capacity of the ArrayList is the length of this array buffer.  ---ArrayList的长度即是数组的长度
    */
    private transient Object[] elementData;

  综上所看,ArrayList底层存储的实现是通过一个数组来实现的

LinkedList

  上源码:

  /**
  * Pointer to first node.
  * Invariant: (first == null && last == null) ||
  * (first.prev == null && first.item != null)
  */
  transient Node<E> first;

  /**
  * Pointer to last node.
  * Invariant: (first == null && last == null) ||
  * (last.next == null && last.item != null)
  */
  transient Node<E> last;

  /**
  * Links e as last element.
  */
  void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
      first = newNode;
    else
      l.next = newNode;
    size++;
    modCount++;
   }

//在ArrayList内部是有这么一个作为"节点"的内部类的

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底层实现是链表;这里代码只是列举了往集合末尾添加元素的情况,具体看linkLast(E e)这个方法

linkLast方法内会根据参数e生成Node,再根据tail具体情况,修改生成node的pre/next指向,存储形式也就是链表啦

  Map:

      map的底层实现是  数组+链表

           描述一下map存储key-value的过程:

             key和value两个对象put到map的时候,会被封装成Entry<K,V>实体对象;put过程会根据K值生成一个hash码值(int类型,不同的key可能会生成相同的hash码)

                   这个hash码会被当成数组的索引/下标(index),数组的每个下标对应一个hash码,而一个hash码对应一个链表(链表存储着具有相同hash码的对象)

                  比如: 现在要  map.put("aa","123");  "aa"对应的hash码是121

                                         map.put("bb","123"); "bb"对应的hash码也是121  执行put操作的时候程序会先到数组找到下标为121(也就是hash的数值)的链表,再通过链表存储put进来的对象

         具体代码:

    

    /**
    * Associates the specified value with the specified key in this map.
    * If the map previously contained a mapping for the key, the old
    * value is replaced.
    *
    * @param key key with which the specified value is to be associated
    * @param value value to be associated with the specified key
    * @return the previous value associated with <tt>key</tt>, or
    * <tt>null</tt> if there was no mapping for <tt>key</tt>.
    * (A <tt>null</tt> return can also indicate that the map
    * previously associated <tt>null</tt> with <tt>key</tt>.)
    */
    public V put(K key, V value) {
      if (key == null)
        return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);  //找出数组对应的下标
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {  //找到数组内对应下标的链表对象
          Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  //原先key已经存在时,覆盖操作
          V oldValue = e.value;
          e.value = value;
          e.recordAccess(this);
          return oldValue;
        }
      }

      modCount++;
      addEntry(hash, key, value, i);//原先不存在key对应的Entry则在链表后添加
      return null;
     }

Set

         set的特点是无序,不重复

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

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

以上是HashSet源码,可以看出HashSet底层是通过Map来实现的,不重复实现:e的hash值相同时,Map会采取覆盖的形式,这样就不会有重复了

Map的无序也就自然导致了HashSet的无序了(hash值求法?HashCode与equals)

"打完收工"....

Java集合类的底层实现探索的更多相关文章

  1. Java集合详解8:Java集合类细节精讲

    今天我们来探索一下Java集合类中的一些技术细节.主要是对一些比较容易被遗漏和误解的知识点做一些讲解和补充.可能不全面,还请谅解. 本文参考:http://cmsblogs.com/?cat=5 具体 ...

  2. java集合类TreeMap和TreeSet

    看这篇博客前,可以先看下下列这几篇博客 Red-Black Trees(红黑树)                                         (TreeMap底层的实现就是用的红黑 ...

  3. Java集合类--温习笔记

    最近面试发现自己的知识框架有好多问题.明明脑子里知道这个知识点,流程原理也都明白,可就是说不好,不知道是自己表达技能没点,还是确实是自己基础有问题.不管了,再巩固下基础知识总是没错的,反正最近空闲时间 ...

  4. Java集合类: Set、List、Map、Queue使用场景梳理

    本文主要关注Java编程中涉及到的各种集合类,以及它们的使用场景 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E ...

  5. Java集合类: Set、List、Map、Queue使用

    目录 1. Java集合类基本概念 2. Java集合类架构层次关系 3. Java集合类的应用场景代码 1. Java集合类基本概念 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的 ...

  6. 基础知识《六》---Java集合类: Set、List、Map、Queue使用场景梳理

    本文转载自LittleHann 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E6%8E%92%E5%BA%8F% ...

  7. java集合类(五)About Map

    接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...

  8. java集合类(四)About Set

    接上篇:java集合类(三)About Iterator & Vector(Stack) 之前,在比较java常见集合类的时候,就了解到一点有关Set的特性.实现类及其要求等,读者可以去温习下 ...

  9. java集合类(三)About Iterator & Vector(Stack)

    接上篇:java集合类学习(二) Talk about “Iterator”: 任何容器类,在插入元素后,还需要取回元素,因为这是容器的最基本工作.对于一般的容器,插入有add()相关方法(List, ...

随机推荐

  1. Silverlight中验证码生成

    public class ValidationCode { Random r = new Random(DateTime.Now.Millisecond); /// <summary> / ...

  2. mybatis mapper.xml的特殊操作符

    select * from test where id<>1; 但是mybatis报错 <> 应该转义  <> select * from test where i ...

  3. django 设置session过期时间

    session的超时时间设置settings中SESSION_COOKIE_AGE=60*30 30分钟.SESSION_EXPIRE_AT_BROWSER_CLOSE False:会话cookie可 ...

  4. linux下open-vswitch安装卸载操作

    一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...

  5. Jmeter+badboy压力测试总结

    流程:badboy导出Jmeter压测脚本 -> Jmeter进行压力测试 软件下载地址: badboy:http://www.badboy.com.au/ Jmeter:http://jmet ...

  6. cf1047C-Enlarge GCD-(欧拉筛+map+gcd+唯一分解定理)

    https://vjudge.net/problem/CodeForces-1047C 题意:有n个数,他们有个最大公约数设为maxxgcd,要删去一些数,使得剩下的数的gcd大于maxxgcd. 解 ...

  7. centos7编译安装nginx

    一.安装依赖包 yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel 二 ...

  8. dubbo常见面试问题(二)

    1.什么是Dubbo? Duubbo是一个RPC远程调用框架, 分布式服务治理框架 2.什么是Dubbo服务治理? 服务与服务之间会有很多个Url.依赖关系.负载均衡.容错.自动注册服务 3.Dubb ...

  9. Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.2.0/gradle-3.2.0.pom'.

  10. Linux - 文件和目录常用命令

    文件和目录常用命令 目标 查看目录内容 ls 切换目录 cd 创建和删除操作 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 ...