I: Overview of the Collective Framework
Collections and arrays are structures for storing and manipulating multiple data, referred to as java containers.
The characteristics of arrays in storing multiple data: once initialized, the length and the type of elements are determined.
Therefore, the shortcomings of storing multiple data are: the length can not be modified; the method provided in the array is very limited, which is very inconvenient and inefficient for adding, deleting and inserting data; the need to obtain the actual number of elements in the array, the array has no ready-made attributes or methods available.
II: Collective Framework
Collection interface: A single column collection used to store one object after another.
/** * @Author: Xiaoxin at will * @Date: 2019/8/20 9:41 * Collection Testing of methods declared in interfaces * CONCLUSION: When adding data objects to the object of the Collection interface implementation class, the class in which the object belongs needs to override the equals() method. */ public class CollectionTest { public void test1(){ Collection coll = new ArrayList ( ); coll.add(123); coll.add ( new Person("tom",20) ); coll.add ( new java.lang.String ( "jenny" ) ); // hashCode (): Returns the hash value of the current object System.out.println (coll.hashCode ()); // Set - --"array: toArray(); Object[] arr = coll.toArray (); for (int i = 0; i < arr.length; i++) { System.out.println (arr[i]); } // Array - "collection, calling the static method of Arrays.asList List<String> strings = Arrays.asList ( new String[]{"aa", "bb"} ); System.out.println (strings); List strings1 = Arrays.asList ( new int[]{123,456} ); System.out.println (strings1); List strings2 = Arrays.asList ( new Integer[]{123,456} ); System.out.println (strings2); } public static void main(String[] args) { CollectionTest collectionTest = new CollectionTest (); collectionTest.test1 (); } }
Result: 1035338341 123 Person{name='tom', age=20} jenny [aa, bb] [[I@7699a589] [123, 456]
The iterator Iterator traversal can only traverse Set sets.
public void test2(){ Collection coll = new ArrayList ( ); coll.add(123); coll.add ( new Person("tom",20) ); coll.add ( new java.lang.String ( "jenny" ) ); // Traversing objects using iterators Iterator iterator = coll.iterator (); while (iterator.hasNext ()){ System.out.println (iterator.next ()); } } public void test3(){ Collection coll = new ArrayList ( ); coll.add(123); coll.add ( new Person("tom",20) ); coll.add ( new java.lang.String ( "jenny" ) ); // for (type local variable of set element: set object) for (Object object:coll){ System.out.println (object); } }
List collection: Stores ordered, repeatable data. ArrayList; LinkedList; Vector
What are the similarities and differences among ArrayList, LinkedList and Vector?
Similarity: All three classes implement List interface, and the characteristics of data storage are the same: orderly and repeatable data storage.
The difference: ArrayList: As the main implementation class of the List interface, threads are insecure and efficient; the underlying uses Object [] storage.
LinkedList: The bottom layer uses a two-way linked list, which is more efficient than ArrayList for frequent insert and delete operations.
Vector: An ancient implementation class as a List interface; thread-safe and inefficient; Object [] storage is used at the bottom.
Set collection: Stores disordered, non-repeatable data. HashSet; TreeSet; LinkedHashSet
Unorderliness: Not randomness; the stored data is not added in the order of the array index in the underlying array, but arranged according to the hash value of the data.
Non-repeatability: Ensure that when adding elements are judged according to equal(), they cannot repent of true, that is, the same element can only be added one.
HashSet: As the main implementation class of Set interface, threads are insecure and efficient.
LinkedHashSet: As a subclass of HashSet.
TreeSet: You can sort by the specified attributes of the added object.
2.2. Map collection:
A two-column collection that stores a key-value pair of data. HashMap; LinkedHashMap; TreeMap; HashTable
HashMap: Threads are insecure and efficient; null key s and value s are stored;
LinkedHashMap: Ensure that when traversing map elements, you can traverse them in the order in which they are added.
Reason: Based on the original HashMap underlying structure, a pair of pointers are added to point to the former and the latter elements.
TreeMap: Ensure sorting according to the added key-value to achieve sorting traversal. Consider the use of red-black trees at the bottom of the key's natural or customized sort.
HashTable: Thread-safe, inefficient, unable to store null key s and value s;
(2) Understanding of map structure
Key in Map: Unordered, non-repeatable, use set to store all keys; the class where key resides needs to override the equal() method and hashCode() method; (Take HashMap as an example)
Value in Map: Unordered, repeatable, using Collection to store all values; the class where value resides needs to override the equal() method
A key-value pair: key-value constitutes an entry object;
Map entry: Unordered, non-repeatable, using Set to store all entries;
(3) The underlying implementation principle of HashMap
1. At jdk7, after HashMap is instantiated, the bottom layer creates a 16-dimensional array Entry[] table;
2. Compared with jdk7, jdk8 does not create an array of 16 length when new HashMap() is called; the bottom array of jdk8 is Node []; when put() method is first called, the bottom layer creates an array of 16 length; the bottom structure of JDK7 is only array + linked list; and the bottom structure of jdk8 includes: array + linked list + red-black tree.
(4) Common methods of HashMap
Map map = new HashMap ( ); map.put ( "aa",123 ); map.put ( "aa",456 ); map.put ( "bb",123 ); // Traverse through all key sets: keySet() Set set = map.keySet (); Iterator iterator = set.iterator (); while (iterator.hasNext ()){ System.out.println (iterator.next ()); } // Traversing through all value elements: values Collection values = map.values (); for (Object object : values){ System.out.println (object); } // Traverse through all key-value:entrySet Set entrySet = map.entrySet (); Iterator iterator1 = entrySet.iterator (); while (iterator1.hasNext ()){ Object object1 = iterator1.next (); Map.Entry entry = (Map.Entry) object1; System.out.println (entry.getKey ()+"--------"+entry.getValue ()); }