Java集合框架底层实现 --源码

List:


  • ArrayList 数组实现
    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * 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 == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    transient Object[] elementData; 

    /**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

 

  • ArrayList 在扩容的时候是采用的 copy 方法:
    /**
     * 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);
    }

  • LinkedList 双向链表
// This is node object.
private static class Node<E> {
            E item;
            LinkedList.Node<E> next;
            LinkedList.Node<E> prev;

            Node(LinkedList.Node<E> prev, E element, LinkedList.Node<E> next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
        }

Set:

  • HashSet 由 HashMap 实现:
    /**
     * 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<>();
    }

  • TreeSet 由 TreeMap 实现:
    /**
     * The backing map.
     */
    private transient NavigableMap<E,Object> m;
    
    /**
     * Constructs a set backed by the specified navigable map.
     */
    TreeSet(NavigableMap<E,Object> m) {
        this.m = m;
    }

  • LinkedHashSet 由 HashSet 实现:
public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable {

   public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }
}

Queue:

  • queue接口不包括并发容器的话常用的实现有 LinkedList 和 ArrayDqueue, 具体的实现结构分别是链表和数组.

Map:

static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next; // 注意此处的 next 引用

        ....省略其他无关代码

 }

  • TreeMap 由链表实现的 二叉排序树实现.

  • LinkedHashMap 继承自 HashMap, 由 HashMap 实现
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种广泛应用于软件开发领域的编程语言,它具有跨平台、面向对象、简单易学等特点。在进行Java毕设的过程中,合理选用一些开的项目集合可以提供很多帮助和参考。 1. GitHub:GitHub是一个知名的代码托管平台,上面有大量开项目的码可以学习和参考。可以搜索关键词,找到与毕设相关的项目,并阅读他人的代码实现。例如,如果毕设是关于图像识别的,可以找到一些相关的开项目,学习他人是如何处理图像识别的。 2. Apache开项目:Apache是一个非营利性的开组织,旗下有多个流行的项目,如Tomcat、Hadoop等。很多项目的码都是开放的,可以通过阅读和学习这些码,了解项目的实现方式和设计思路,从而应用到自己的毕设中。 3. Stack Overflow:Stack Overflow是一个程序员常用的问答网站,上面有很多Java相关的问题和解答。可以通过搜索或提问的方式获取问题的解决方案,以及一些参考的码。 4. Java框架Java有很多流行的开框架,如Spring、Hibernate等。阅读框架码可以了解框架底层实现和使用方式,可以借鉴其中的设计思想和编码规范。 5. 技术博客和论坛:在互联网上,有很多技术大牛都有自己的技术博客或者活跃在一些技术论坛中,他们经常分享自己的经验和代码。可以通过阅读他们的博客或者参与讨论,学习到一些实用的码和技术知识。 以上是一些可以获取Java毕设码的途径,通过学习和参考这些码,可以提高自己的编程能力和项目实践经验,提升毕设的质量和完成度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值