`
salever
  • 浏览: 255731 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java看书笔记

 
阅读更多

这一篇专用于一些日常的Java读书笔记

 

先写一点关于String的,

  1. String对象直接赋值比较好,因为Java里面有一个String常量池的玩意,可以节省内存空间
  2. String对象是immutable的,不能被修改
  3. String.subString(0) 其实会返回自己(==判断为true,对象地址为同一个)
  4. String.intern() 在某些情况下会返回一样的常量池对象(当String为常量池对象的时候)
public static void main(String[] args)
    {
        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        String s4 = new String(s1);
        String s5 = s1.intern();
        String s6 = s3.substring(0);

        String s7 = new String("d");
        String s8 = s7.intern();

        System.out.println("s1 == s2 " + (s1 == s2));
        System.out.println("s1 == s3 " + (s1 == s3));
        System.out.println("s1 == s4 " + (s1 == s4));
        System.out.println("s1 == s5 " + (s1 == s5));
        System.out.println("s3 == s6 " + (s3 == s6));
        System.out.println("s7 == s8 " + (s7 == s8));
    }

 结果:

写道
s1 == s2 true
s1 == s3 false
s1 == s4 false
s1 == s5 true
s3 == s6 true
s7 == s8 false 
分享到:
评论
4 楼 salever 2014-08-08  
HashMap的几个注意点
1,key的hash尽量不要冲突,否则HashMap将会将hash冲突的元素存到一个链表中,这会大大降低效率

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;
            //精确的判断key的hash和==以及equals方法是否一样,然后替换
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        //添加元素,如果对应的下表元素存在,将会存储为链表
        addEntry(hash, key, value, i);
        return null;
    }

2,HashMap的长度为2次幂,这是因为这样的话下标的计算结果冲突的概率最低,
   /**
     * Returns index for hash code h.
     */
    static int indexFor(int h, int length) {
        //2次幂保证了下标计算的唯一性
        return h & (length-1);
    }

3,HashMap的resize代价很高,需要重新分配空间,然后移动所有的元素,包括计算下标
    void transfer(Entry[] newTable) {
        Entry[] src = table;
        int newCapacity = newTable.length;
        for (int j = 0; j < src.length; j++) {
            Entry<K,V> e = src[j];
            if (e != null) {
                src[j] = null;
                do {
                    Entry<K,V> next = e.next;
                    int i = indexFor(e.hash, newCapacity);
                    e.next = newTable[i];
                    newTable[i] = e;
                    e = next;
                } while (e != null);
            }
        }
    }
3 楼 salever 2014-08-07  
ArrayList VS LinkedList,前者的set效率高,后者的插入和删除效率高,添加操作两者几乎相当(除非ArrayList需要频繁的扩容)
2 楼 salever 2014-08-07  
ArrayList的foreach方式遍历,比数组下标方式慢很多,因为它是RandomAccess的,而foreach则强制让它实现了iterator
1 楼 salever 2014-08-07  
指定ArrayList的初始化大小,有可能为你带来更好的性能,主要是避免频繁的进行扩容和复制操作

public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
    Object oldData[] = elementData;
    int newCapacity = (oldCapacity * 3)/2 + 1;
        if (newCapacity < minCapacity)
newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
}
    }

相关推荐

Global site tag (gtag.js) - Google Analytics