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. 记一次laravel-jwt修改黑名单所用redis数据库

    场景是这样的,我用tymon/jwt包做鉴权.jwt是自编码token,过期前想要强制失效只能将其加入黑名单中,黑名单一般用缓存存储. 但会有一个问题,若某种意外情况不小心执行了php aritsan ...

  2. 从 TFS 迁移源代码到 git

    准备工具: https://github.com/git-tfs/git-tfs 具体的安装步骤上面的 readme.md 中有说明.通过 Chocolatey 安装,如果本地没有 git ,会自动安 ...

  3. Linux服务器部署javaweb项目,从环境配置,到最终系统运行

    部署准备:javaJDK1.7,Tomcat7.0,MySQL5.8,可运行的javaWeb项目,linux环境的服务器(可以是安装在windows电脑上的linux虚拟机,安装了linux系统的电脑 ...

  4. vue+el-menu设置了router之后如何跳转到外部链接

    <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="c ...

  5. angluar1+ionic详情页返回在原来的位置(缓存数据和页面高度)

    因为是老项目,近期开发遇到了个需求就是从详情页按返回按钮要求返回到原来列表的页面位置,刚开始准备用的cache:true,但是存在大大的问题就是新增和编辑后返回数据都不是最新的,无法重新刷新页面rel ...

  6. phpstudy设置允许远程访问mysql数据库

    1.先在服务器中通过命令行方式(打开phpstudy界面->右下角其他菜单选项->MySQL工具->MySQL命令行) 登录mysql:mysql   -u root -p 密码 ( ...

  7. vue 下拉刷新 上拉加载(vue-scroller)

    <template> <div class="wdRecordCon"> <x-header :left-options="{backTex ...

  8. 使用Vue-Router路由

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  9. CodeWarrior 10 添加/修改 头文件路径

    当使用CodeWarrior 10时,默认使用大名鼎鼎的GCC编译器. 我们在构建工程的时候,往往按模块分类文件夹,那么就存在需要包含头文件路径的问题.那么如何加入头文件的路径呢?见下文. 1.打开工 ...

  10. rancher2.1.7 +jenkins +harbor 自动容器CI系统(通过rancher命令行)

    jenkins脚本执行示例: //环境定义与 cd $WORKSPACEmodule=news-usercd $module/case $deploy in deploy) //发布模块 //编译/o ...