|-- Map: double column data, which stores the data of key value pairs,
| -- HashMap: as the main implementation class of Map, the thread is unsafe and efficient, and stores null key and value
| -- LinkedHashMap: ensure that when traversing elements, traversal is implemented in the order of addition
Principle: on the basis of the original HashMap underlying structure, add a pile of pointers, frequently traverse operations, and the execution efficiency is higher than that of HashMap
| -- TreeMap: ensure sorting according to the added key value to realize sorting traversal. At this time, consider the natural sorting and customized sorting of keys, and the red black tree is used at the bottom
| -- Hashtable: as an ancient implementation class, it is thread safe and inefficient, and cannot store null key s and value s
| -- Properties: commonly used to process configuration files. Both key and value are String types
Map structure
Keys in Map: unordered and non repeatable. Use Set to store all keys -- > the class where the key belongs should override equals() and hashcode() (take HashMap as an example)
Value in Map: unordered and repeatable. Use Collection to store all values. Override equals() in the class where value is located
A key value pair: key value constitutes an Entry object
Entries in Map: unordered and non repeatable. Set is used to store all entries
Output the data in the map through the iterator: Map - > Set - > iterator
- Call Map Entryset() method to convert a Map Set into a Set set, where the generic type in the Set is: Map Entry<K,V>;
- Call Set The Iterator () method converts the Set set into an Iterator iterator, and the generic type is still: map Entry<K,V>;
- Call the hasNext() and next() methods of Iterator to fetch the map in a loop Entry < K, V > interface object;
- Call map getKey() of the entry interface gets the key and getValue() gets the value.
=========================================================================
HashMap
The bottom layer of HashMap: jdk7{array and linked list
jdk8 and later arrays plus linked lists plus red and black trees
=========================================================================
TreeMap
Because a key value is added to the TreeMap, it is required that the key must be an object created by the same class
Because you need to sort by key, natural sorting and custom sorting
because TreeMap Add in key-value,requirement key Objects that must be created by the same class Because according to key Sort, natural sort, custom sort @Test public void treeMapTest1() { TreeMap map = new TreeMap((o1, o2) -> { if (o1 instanceof StudentSet && o2 instanceof StudentSet) { StudentSet s1 = (StudentSet) o1; StudentSet s2 = (StudentSet) o2; return Integer.compare(s1.getAge(), s2.getAge()); } else { throw new RuntimeException("error in type"); } }); StudentSet st1 = new StudentSet(13, "lucky"); StudentSet st2 = new StudentSet(10, "jack"); StudentSet st3 = new StudentSet(23, "Tom"); map.put(st1, 89); map.put(st2, 78); map.put(st3, 100); // Mode 1 Set entrySet = map.entrySet(); Iterator a = entrySet.iterator(); while (a.hasNext()) { Object obj = a.next(); Map.Entry entry = (Map.Entry) obj; System.out.println(entry.getKey() + "---->" + entry.getValue()); } // Mode II Set keySet = map.keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = map.get(key); System.out.println(key + "----" + value); } }
=========================================================================
Hashtable
The default size of hash array in HashTable is 11, and the increase method is {old*2+1
HashTable locks the entire hash table and uses the synchronized synchronization method, so its performance is very low;
@Test public void propertiesTest() { FileInputStream fileInputStream=null; try { Properties properties = new Properties(); fileInputStream= new FileInputStream("jabc.properties"); //Load the file corresponding to the stream properties.load(fileInputStream); String name=properties.getProperty("name"); String password=properties.getProperty("password"); System.out.println(name+"---"+password); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }