Integer and String

1.1 the Integer

A wrapper class is a wrapper around a basic data type, with some commonly used methods

For example, type replacement and so on.

An int wrapper class that uses final modifiers and is not inheritable.

Automatic packing and unpacking.

Note that between -128 and 127, an array is maintained (static initialization exists when the server starts).

When comparing values, if they are in this range, the default value is taken from the array.

Integer a = 1;
Integer b = 1;
Integer c = 128;
Integer d = 128;
sout(a == b); // true
sout(c == d); // false

The source code section

private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue ! = null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache. HIGH >= 127; } private IntegerCache() {} } public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }

1.2 the String

An immutable string.

When String concatenation is used, the append method of StringBulider is called by default

String str1 = "hello,"; String str2 = "dbc"; String str3 = str1 + str2; String str3 = (new StringBuilder()).append(str1).append(str2).toString(); String str3 = (new StringBuilder()).append(str2).toString();

The difference between String, StringBuilder, StringBuffer

  1. String: String of fixed length
  2. StringBuilder: Variable length string, linearly unsafe, high efficiency
  3. StringBuffer: Variable length string, linear safety, low efficiency

2. The List

Ordered and repeatable

2.1 ArrayList

An array is maintained at the bottom

According to the number and capacity of stored data, it will automatically expand the capacity

Features: Quick queries, slow add and delete (compared to LinkedList).

The source code

transient Object[] elementData; // non-private to simplify nested class access private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; 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); }} public Boolean add(E E) {EnsureCapityInternal (size + 1); // Increments modCount!! elementData[size++] = e; return true; } // private void EnsureCapityInternal (int MinCapacity) {EnsureExplicitCapacity (Calculatecapacity (ElementData, Calculatecapacity); minCapacity)); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } 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); }

2.2 LinkedList

A linked list is maintained at the bottom

Features: Slow queries, fast add and delete (compared to ArrayList).

The source code

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

Three. The Map

Key-value storage format

Key is not repeatable

3.1 a HashMap

At the bottom is a hash table (array + linked list)

Key and value can be null

Differences with Hashtable

  1. Hashtable is linearly safe and inefficient, while HashMap is linearly insecure.
  2. Hashtable does not allow NULL values (key and value). HashMap values can be NULL

The source code

Static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; Static final int MAXIMUM_CAPACITY = 1 << 30; static final int MAXIMUM_CAPACITY = 1 << 30; Static final int TREEIFY_THRESHOLD = 8; static final int TREEIFY_THRESHOLD = 8; static final int TREEIFY_THRESHOLD = 8; Static final int MIN_TREEIFY_CAPACITY =64; static final int MIN_TREEIFY_CAPACITY =64; static final int MIN_TREEIFY_CAPACITY =64; static final int MIN_TREEIFY_CAPACITY =64; Static final int UNTREEIFY_THRESHOLD = 6; static final int UNTREEIFY_THRESHOLD = 6; Static final float DEFAULT_LOAD_FACTOR = 0.75f; static final float DEFAULT_LOAD_FACTOR = 0.75f; static final float DEFAULT_LOAD_FACTOR = 0.75f; static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * static class Node<K,V bb0 implements Map.Entry<K,V> {final int hash; final K key; V value; Node<K,V> next; Node(int hash, K key, V value, Node<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final K getKey() { return key; } public final V getValue() { return value; } public final String toString() { return key + "=" + value; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry<? ,? > e = (Map.Entry<? ,? >)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; }}

The four.

Disordered, not repeatable

A Map is maintained at the bottom

When a value is saved, the added value is stored as the key of the Map, and the value is an Object

So to get a value, you can only get an iterator to traverse

4.1 HashSet

A HashMap is maintained at the bottom

When the value is saved, it is the key of the Map

NULL can be added (HashMap keys can be NULL)

How does a HashSet determine if an element is duplicated?

  1. First determine hashCode
  2. Use equals

If hashCode returns the same value and equals returns true, it is considered the same element

The source code

. private transient HashMap<E,Object> map; Private static final Object PRESENT = Dummy value to associate with an Object in the backing Map new Object(); . Public HashSet() {map = new HashMap<>(); }... Public Boolean add(E E) {return map.put(E, PRESENT)==null; }

4.2 LinkedHashSet

A subclass of HashSet

Two-way linked list

4.3 the TreeSet

A TreeMap is maintained at the bottom

When the value is saved, it is the key of the Map

Cannot add null

The source code

private transient NavigableMap<E,Object> m; TreeSet(NavigableMap<E,Object> m) { this.m = m; } public TreeSet() {this(new TreeMap<E,Object>()); } public Boolean add(E E) {return m.put(E, PRESENT)==null; }