• 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));
        }
    

      

  • 相关阅读:
    Oracle自带的sql developer导入导出数据
    LSMW批处理工具操作手册
    JS控制TABLE表格在任意一行下面添加一行(有待完善)
    SAP销售模块塑工常见问题和解决方案(自己收藏)
    SAP采购订单历史明细报表源代码(自己收藏)
    SAP公司间采购订单关联交货单报表源代码(自己收藏)
    《嵌入式系统可靠性设计技术及案例解析》读书笔记(七)
    《嵌入式系统可靠性设计技术及案例解析》读书笔记(六)
    《嵌入式系统可靠性设计技术及案例解析》读书笔记(五)
    《嵌入式系统可靠性设计技术及案例解析》读书笔记(四)
  • 原文地址:https://www.cnblogs.com/zhizhiyin/p/11687540.html
Copyright © 2020-2023  润新知