Arrays and Collections

array part

  • 1. Array definition: class name [] array name, the data of this type is stored in the array;


  • 2. Two-dimensional array:

    • In the definition of a two-dimensional array, the first bracket must have a value, which cannot be empty, but can be 0;


  • 3. The initialization method of the array:

  • static initialization

int [] a = {1,2,5,7,3};    //静态初始化基本类型数组
User[] b = {
    new User(01,"张三"),
    new User(02,"李四"),
    new User(03,"王五")
};                     //静态初始化引用类型数组;
  • Dynamic initialization

int[] a = new int[2];   //动态初始化数组,先分配空间;
a[0] = 1;//给数组元素赋值;
a[1] = 2;//给数组元素赋值;
  • default initialization

int[] a = new int[2];   //默认值:0,0;
boolean[] b = new boolean[2];   //默认值:false,false;
String[] s = new String[2];     //默认值:null,null;

  • 4. Legal initialization of a two-dimensional array:

// 数据类型[][] 数组名;
int [][] table = new int[2][2];
int [][] table = new int[2][];
int [] table [] = new int[2][2];
int [] table [] = new int[2][];

  • 5. The difference between an array and a collection:

  • (1) The length of the array is fixed and immutable;

  • (2) The collection can change dynamically at runtime;


  • 6. Collection framework architecture in java:

image


Collection section

  • 1. Iterator traverses the collection:

Interator it = List.interator();
while(it.hasNext()){
    
}

  • 2. Before the concept of generics was introduced (Java SE 1.5 introduced the concept of generics), objects stored in collections became Objects, and type conversion was required when taking them out;


  • 3、ArrayList和LinkedList

    • Both ArryList and LinkedList implement the List interface. The memory structure of ArrayList is an array, which is essentially a linear list of sequential storage. Insertion and deletion operations will cause subsequent elements to move, which is inefficient, but random access is efficient;

    • The memory structure of LinkedList is stored in a doubly linked list. The chained storage structure has high insertion and deletion efficiency and does not need to be moved. However, the random access efficiency is low, and it needs to be accessed from the beginning to the back.

  • 4、HashTable和HashMap:

    • Both HashMap and Hashtable classes implement the Map interface, and both store KV pairs (key-value pairs);

    • HashTable does not allow null values ​​(neither key nor value), HashMap allows null values ​​(both key and value);

    • Hashtable's method is Synchronize, but HashMap is not. When multiple threads access Hashtable, you do not need to synchronize its methods yourself, and HashMap must provide external synchronization for it;

    • The iterators returned by all the "collection view methods" of the HashMap class are fail-fast: if the map is structurally modified after the iterator is created, any time or any other way, except through the remove method of the iterator itself Modifications of the iterator will throw ConcurrentModificationException. The main difference between Hashtable and HashMap is that the former is synchronous, and the latter is guaranteed by a fail-fast mechanism.

  • 5. Some statements about HashMap:

    • a) HashMap is actually a "linked list hash" data structure, that is, a combination of an array and a linked list. The underlying structure of HashMap is an array, and each item in the array is a linked list.
    • b) An instance of HashMap has two parameters that affect its performance: the "initial capacity" and the fill factor.
    • c) HashMap implementation is not synchronized and thread-unsafe. HashTable thread safety
    • d) The key-values ​​in HashMap are all stored in Entry.
    • e) HashMap can store null keys and null values. It does not guarantee that the order of elements will remain unchanged. Its bottom layer uses arrays and linked lists, and the uniqueness of keys is guaranteed by the hashCode() method and the equals method.
    • f) There are three main methods for resolving conflicts: addressing method, zipper method, and re-hash method. HashMap uses the zipper method to resolve hash collisions.
    • Note: The linked list method is to form a linked list of objects with the same hash value and place them in the slot corresponding to the hash value;
    • The practice of using the open addressing method to resolve conflicts is: when a conflict occurs, a probing (probing) sequence is formed in the hash table using a certain probing (also called probing) technique. Search cell by cell along this sequence until the given keyword is found, or an open address is encountered (that is, the address cell is empty) (if you want to insert, when an open address is detected, you can insert the address to be inserted. The new node is stored in this address unit).
    • The zipper method for conflict resolution is:
      • Link all nodes whose keywords are synonyms in the same singly linked list. If the length of the selected hash table is m, the hash table can be defined as a pointer array T[0..m-1] consisting of m head pointers. All nodes whose hash address is i are inserted into the singly linked list with T[i] as the head pointer. The initial value of each component in T should be a null pointer. In the zipper method, the loading factor α can be greater than 1, but generally α≤1. The zipper method fits the size of unspecified elements.
  • 6. The difference between Hashtable and HashMap:

    • a) Inheritance is different.

public class Hashtable extends Dictionary implements Map

public class HashMap extends AbstractMap implements Map

    • b) The methods in Hashtable are synchronous, while the methods in HashMap are asynchronous by default. In a multi-threaded concurrent environment, Hashtable can be used directly, but if you want to use HashMap, you need to add synchronization processing yourself.
    • c) In Hashtable, null values ​​are not allowed for both key and value. In HashMap, null can be used as a key, and there is only one such key; there can be one or more keys corresponding to null. When the get() method returns a null value, it means that the key does not exist in the HashMap, or the value corresponding to the key is null. Therefore, in HashMap, the get() method cannot be used to judge whether a key exists in the HashMap, but the containsKey() method should be used to judge.
    • d) The internal implementation of the two traversal methods is different. Both Hashtable and HashMap use Iterator. For historical reasons, Hashtable also uses Enumeration.
    • e) The use of hash values ​​is different, HashTable directly uses the hashCode of the object. And HashMap recalculates the hash value.
    • f) The initial size and expansion method of the array of Hashtable and HashMap their two internal implementations. The default size of the hash array in HashTable is 11, and the way to increase it is old*2+1. The default size of the hash array in HashMap is 16, and it must be an index of 2.
    • Note: HashSet subclasses rely on the hashCode() and equal() methods to distinguish duplicate elements.

      • HashSet internally uses Map to save data, that is, the data of HashSet is stored as the key value of Map, which is why the elements in HashSet cannot be repeated. If the key value is stored in the Map, it will judge whether the current Map contains the Key object. Internally, the hashCode of the key is used to determine whether it has the same hashCode, and then the equals method is used to judge whether it is the same.
  • 7. The use of hashMap in a single thread greatly improves the efficiency, and hashTable is used to ensure safety in the case of multiple threads;

    • The synchronized keyword is used in hashTable to implement the security mechanism. Synchronized is to lock the entire hash table, that is, to allow the thread to exclusively share the entire hash table, which is a waste of safety and at the same time;
    • concurrentHashMap uses a segmented locking mechanism to ensure safety.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325068293&siteId=291194637