• java的一些代码阅读笔记


    读了一点源码,很浅的那种,有些东西觉得很有趣,记录一下。

    • ArrayList的本质是Object[]
     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);
            }
        }
    

      

    • HashSet的本质是HashMap
      public HashSet() {
            map = new HashMap<>();
        }
    
    •  HashMap底层是数组+node
        final Node<K,V> getNode(int hash, Object key) {
            Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
            if ((tab = table) != null && (n = tab.length) > 0 &&
                (first = tab[(n - 1) & hash]) != null) {
                if (first.hash == hash && // always check first node
                    ((k = first.key) == key || (key != null && key.equals(k))))
                    return first;
                if ((e = first.next) != null) {
                    if (first instanceof TreeNode)
                        return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            return e;
                    } while ((e = e.next) != null);
                }
            }
            return null;
        }
    
        /**
         * Returns <tt>true</tt> if this map contains a mapping for the
         * specified key.
         *
         * @param   key   The key whose presence in this map is to be tested
         * @return <tt>true</tt> if this map contains a mapping for the specified
         * key.
         */
        public boolean containsKey(Object key) {
            return getNode(hash(key), key) != null;
        }
    

      

     public V put(K key, V value) {
            return putVal(hash(key), key, value, false, true);
        }
    
        /**
         * Implements Map.put and related methods
         *
         * @param hash hash for key
         * @param key the key
         * @param value the value to put
         * @param onlyIfAbsent if true, don't change existing value
         * @param evict if false, the table is in creation mode.
         * @return previous value, or null if none
         */
        final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
            Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);
            else {
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {
                    for (int binCount = 0; ; ++binCount) {
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();
            afterNodeInsertion(evict);
            return null;
        }
    

      

     

    3.String的本质是final类型的char[]

    private final char value[];
    public String() {
            this.value = "".value;
        }
    public String(String original) {
            this.value = original.value;
            this.hash = original.hash;
        }
    public String(char value[]) {
            this.value = Arrays.copyOf(value, value.length);
        }
    

      

    4.StringBuffer和StringBuilder都继承自AbstractStringBuilder

    public final class StringBuffer
        extends AbstractStringBuilder
        implements java.io.Serializable, CharSequence
    
    
    public final class StringBuilder
        extends AbstractStringBuilder
        implements java.io.Serializable, CharSequence
    

    5.StringBuffer和StringBuilder的主要差别在StringBuffer里面由很多Synchronize操作,StringBuilder里面没有。就是一般说的前者线程安全。

    StringBuffer:

     @Override
        public synchronized StringBuffer append(Object obj) {
            toStringCache = null;
            super.append(String.valueOf(obj));
            return this;
        }  

    StringBuilder:

        @Override
        public StringBuilder append(Object obj) {
            return append(String.valueOf(obj));
        }
    

      

  • 相关阅读:
    solr两种启动方式
    Centos7安装Solr
    关于netcore webservice 构建和参数大小控制问题
    百度开源ocr安装(飞浆)
    C# netcore 开发WebService(Soap)
    Aspose.CAD库简介 CAD转换PDF
    .net或JAVAK开发如何实现上传文件夹功能
    【转】TCP和SOCKET关系
    Asp.Net 绑定下拉框的值
    Windwos服务器远程桌面突然不能复制粘贴
  • 原文地址:https://www.cnblogs.com/zhizhiyin/p/11687540.html
Copyright © 2020-2023  润新知