Array part
- 1, the array definition: the class name [] array name, the array to hold the type of data;
- 2, two-dimensional array:
-
-
In the definition of a two-dimensional array, the first parenthesis must have a value, cannot be empty, but can be 0;
- 3, the initialization of the array mode:
-
Static initialization
int [] a = {1,2,5,7,3}; //静态初始化基本类型数组User[] b = { new User(01,"张三"), new User(02,"李四"), new User(03,"王五")}; //静态初始化引用类型数组;
int[] a = new int[2]; //动态初始化数组,先分配空间;a[0] = 1;//给数组元素赋值;a[1] = 2;//给数组元素赋值;
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 two-dimensional arrays:
// 数据类型[][] 数组名;int [][] table = new int[2][2];int [][] table = new int[2][];int [] table [] = new int[2][2];int [] table [] = new int[2][];
- 5. Differences between arrays and collections:
-
(1) The array length is fixed and immutable;
-
(2) The set can change dynamically at run time;
- 6. The architecture of the collection framework in Java:
Collection section
- 1. Iterate through the collection:
Interator it = List.interator();while(it.hasNext()){ }
- 2, before the introduction of the concept of generics (Java SE 1.5 introduced the concept of generics), the object is stored in the collection is an object, the need for type conversion;
- 3, ArrayList and LinkedList
-
-
Arrylist and LinkedList both implement the list interface, the memory structure of ArrayList is an array, the essence is sequential storage of linear table, insert and delete operations will cause the subsequent elements to move, inefficient, but the efficiency of random access is high;
-
-
LinkedList memory structure is a two-way linked list storage, chain storage structure insertion and deletion of high efficiency, do not need to move, but the efficiency of random access is low, you need to go back from the beginning
- 4, Hashtable and HashMap:
-
-
HashMap and Hashtable two classes have implemented the map interface, the two save K-v pair (Key-value pair);
-
-
Hashtable does not allow null values (neither key nor value), HashMap allows null values (both key and value can be);
-
-
Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, do not need their own method to achieve synchronization, and HashMap must provide for the external synchronization;
-
-
The iterator returned by the collection view method of all the HashMap classes is a quick failure: After the iterator is created, if the mappings are modified from the structure, except through the Remove method of the iterator itself, any other modification at any time, Iterators will throw concurrentmodificationexception. The main difference between Hashtable and HashMap is that the former is synchronous, and the latter is the guarantee of fast failure mechanism.
- 5. Some statements about HashMap:
-
- A) HashMap is actually a "chain-table hash" of the data structure, that is, the combination of arrays and linked lists. The underlying structure of the HASHMAP is an array, and each item in the array is a linked list.
-
- b) Examples of HashMap have two parameters that affect their performance: "Initial capacity" and filling factor.
-
- c) HashMap implementation of the different steps, thread is not secure. Hashtable thread Safety
-
- d) key-value in HashMap are stored in entry.
-
- e) HashMap can deposit null keys and null values, does not guarantee that the order of elements is constant, its underlying use of arrays and linked lists, through the Hashcode () method and the Equals method to ensure the uniqueness of the key
-
- f) There are three main ways to resolve conflicts: Addressing method, Zipper method, and re-hashing method. HashMap is the use of zipper method to solve the conflict of the hash.
-
- Note: The linked list method is the same hash value of the object composed of a list placed in the hash value corresponding slot;
-
- The approach to conflict resolution with open addressing is to use some sort of probing (also known as probing) technique to form a sniffing sequence in a hash table when a conflict occurs. Finds the specified keyword along this sequence, either until a given key is found, or when an open address (that is, the address cell is empty) (to insert, in the case of an open address, the new node to be inserted is stored in the Address cell).
-
- The zipper approach to conflict resolution is:
-
-
- Link all keywords to synonyms in the same single-linked list. If the hash list length selected is M, the hash list can be defined as an array of pointers consisting of M head pointers T[0..m-1]. All nodes with hash address I are inserted into a single 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 filling factor α can be greater than 1, but generally take α≤1. The Zipper method fits the size of the unspecified element.
- 6. The difference between Hashtable and HashMap:
-
- A) different inheritance.
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 not synchronized by default. In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process.
-
- c) in Hashtable, both key and value do not allow null values. In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, it can indicate that the key is not in the HASHMAP, or that the value corresponding to the key is null. Therefore, the get () method cannot be used in HASHMAP to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.
-
- D) The internal implementation of the two traversal modes is different. Hashtable and HashMap all use the iterator. For historical reasons, Hashtable also used the enumeration approach.
-
- e) The hash value is used differently, Hashtable directly uses the object's hashcode. The hash value is recalculated by the HashMap.
-
- f) Hashtable and hashmap their two internal implementations in the form of an array of initial size and expansion. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.
-
-
Note: The HashSet subclass relies on the hashcode () and equal () methods to differentiate between repeating elements.
-
-
- HashSet internal use map to save data, will be hashset data as the key value of the map to save, which is the reason why elements in hashset cannot be duplicated. and the map to save the key value, will be to determine whether the current map contains the key object, the interior is first through the key hashcode, determined to have the same hashcode, and then by the Equals method to determine whether the same.
- 7, HashMap in single-threaded use greatly improve efficiency, in the case of multi-threaded use of Hashtable to ensure security;
-
- Hashtable use the Synchronized keyword to implement security mechanism, synchronized is to lock the whole hash table is to allow the thread to enjoy the entire hash table, in the security and caused a waste;
-
- Concurrenthashmap uses a segmented locking mechanism to ensure security.
Arrays and collections