Article directory
-
- Map: a collection of multiple key-value pairs
- 1. Overall architecture process
- 2.Map
-
- 2.1 Common methods
- 2.2 Traversal method:
- 3.TreeMap
-
- 3.1 New method
- 3.2 Sorting
-
- 3.2.1. Internal Comparator
- 3.2.2 External Comparator
- 4. HashMap
-
- 4.1 Common methods are the same as Map
- 5. Hashtable
-
- 5.1 Differences from HashMap:
- 6.Properties:
Map: a collection of multiple key-value pairs
The implementation classes of Map include:
HashMap
TreeMap
Hashtable
Properties
1. Overall architecture process
For example:
In the language model, the encoder and decoder are formed by splicing Transformer components one by one.
2.Map
Key-value pair <key,value>: mapping relationship, one-to-one correspondence: a key can only correspond to a value The key is unique and unordered --->Set value can be repeated, unordered --->Collection Deduplication: the key is the same, and the value is overwritten
2.1 Common methods
* V put(K key, V value) * void clear() * map. clear(); * boolean isEmpty() * boolean containsKey(Object key) * boolean containsValue(Object value) * V get(Object key)-------Returns the value to which the specified key is mapped, or null if this map does not contain a mapping for the key. * static <K, V> Map<K,V> of(K k1, V v1, K k2, V v2)-----Returns an unmodifiable map containing two maps. * V remove(Object key) ------ removes the key's mapping from this mapping, if present (optional operation) * default V replace(K key, V value) * default boolean replace(K key, V oldValue, V newValue) * int size()
2.2 Traversal method:
1.values() Get the values of all key-value pairs 2.keySet() returns the key of all key-value pairs 3.Set<Map.Entry<K,V>> entrySet() Returns the Set view of the mappings contained in this map.
///1.values() method traverses the map to get the value Collection<String> value = map. values(); for(String values:value){<!-- --> System.out.println(values); } 2. The keySet() method traverses the map to get the key Set<Integer> keys = map. keySet(); for(Integer key:keys){<!-- --> System.out.println(key); } ///3.Set<Map.Entry<K,V>> entrySet() Returns a Set view of the mappings contained in this map. Set<Map.Entry<Integer,String>> entries = map.entrySet(); for(Map.Entry<Integer,String> entry:entrys){<!-- --> System.out.println(entry.getKey() + "----->" + entry.getValue()); }
3.TreeMap
There is a new method: TreeMap<k,v> tree = new TreeMap<>();
3.1 New method
*ceilingKey--- returns the smallest key greater than or equal to the given key, or null if there is no such key *ceilingEntry(K key) --- Returns the key-value map associated with the smallest key greater than or equal to the given key, or null if there is no such key. *floorKey(K key) --- returns the smallest key less than or equal to the given key, or null if there is no such key *floorEntry(K key) --- returns the smallest key less than or equal to the given key, or null if there is no such key *higherEntry(K key) --- Returns the key-value map associated with the smallest key strictly greater than the given key, or null if there is no such key. *higherKey(K key) --- Returns the smallest key strictly greater than the given key, or null if there is no such key. *lowerEntry(K key) --- Returns the key-value map associated with the largest key strictly less than the given key, or null if there is no such key. *lowerKey(K key) --- returns the largest key strictly less than the given key, or null if there is no such key *firstEntry() --- Returns the key-value map associated with the smallest key in this map, or null if the map is empty. *K firstKey() --- Returns the first (lowest) key currently in this map. *lastEntry() --- Returns the key-value map associated with the largest key in this map, or null if the map is empty. *lastKey()---/ Returns the last (highest) key currently in this map. *pollFirstEntry() --- Removes and returns the key-value map associated with the smallest key in this map, or null if the map is empty. *pollLastEntry() --- Removes and returns the key-value map associated with the largest key in this map, or null if the map is empty.
3.2 Sorting
3.2.1. Internal Comparator
3.2.2 External Comparator
TreeMap<Double, String> tree = new TreeMap<>(); Teacher tea = new Teacher("a","Math"); Teacher tea2 = new Teacher("b","IT"); TreeMap<Teacher, String> tree1 = new TreeMap<>(); tree1. put(tea, tea. getType()); tree1.put(tea2, tea2.getType()); System.out.println(tree1); //----------internal TreeMap<Teacher,String> tree2 = new TreeMap<>((o1, o2)-> o1.getType().compareTo(o2.getType())); tree2. put(tea, tea. getType()); tree2. put(tea2, tea2. getType()); } } class tem implements Comparator<Teacher>{<!-- --> @Override public int compare(Teacher o1, Teacher o2) {<!-- --> return o1.getType().compareTo(o2.getType()); } } class Teacher implements Comparable<Teacher>{<!-- --> private String name; private String type; public Teacher(){<!-- -->} public Teacher(String name, String type) {<!-- --> this.name = name; this.type = type; } public String getName() {<!-- --> return name; } public void setName(String name) {<!-- --> this.name = name; } public String getType() {<!-- --> return type; } public void setType(String type) {<!-- --> this.type = type; } @Override public String toString() {<!-- --> return "Teacher{" + "name='" + name + ''' + ", type='" + type + ''' + '}'; } @Override public int compareTo(Teacher o) {<!-- --> return this.type.compareTo(o.type); } }
4. HashMap
Map<Integer,String> map = new HashMap<>();
4.1 Common methods are the same as Map
Map<Integer, String> map = new HashMap<>(); map. put(01,"a"); map. put(02,"b"); map.replace(01,"c");//key remains unchanged, replace value System.out.println(map); System.out.println(map.containsKey(01)); System.out.println(map.containsValue("c")); Map map1=Map.of(03,"d",04,"e");//immutable System.out.println(map1); System.out.println(map.size()); map.replace(01,"c","g"); System.out.println(map);
5. Hashtable
The difference between 5.1 and HashMap:
The way to calculate the hash value is different | Inherited from the parent class | Allow null keys and null values | Thread safety/ Synchronization problem | Initial capacity | Expansion mechanism |
---|---|---|---|---|---|
HashMap | java .util.AbstractMap<> | A key can only have one null, and a value can have multiple nulls | Unsafe/not synchronized | Initial capacity 16/ Specified by the constructor | Expansion critical value 12 (16*0.75), each expansion is twice the original capacity/specified by the constructor |
Hashtable | java.util.Dictionary<> | Not possible | safe/synchronous | initial capacity 11/specified by constructor | Each expansion is twice the original capacity + 1/specified by the constructor |
Hashtable is not recommended.
6. Properties:
The Properties class represents a set of persistent properties. Can be saved to or loaded from a stream. Each key and its corresponding value in the property list is a string. Read key-value pair data from the configuration file through Properties: 1. Define configuration file.properties, store key-value pair data 2. Build Properties object in java 3. Call load on the Properties object to establish a connection with the input stream 4. Use getProperty to get the value according to the key to read the key-value pair data from the configuration file
// read and write operations Properties properties = new Properties(); properties.setProperty("1","a"); properties.setProperty("2","b"); properties.setProperty("3","c"); System.out.println(properties); System.out.println(properties.get("1")); //Output stream, output (write) key-value pair data to file .properties, store() OutputStream output =new BufferedOutputStream(new FileOutputStream("src//entry.properties")); properties.store(output,"comment"); output. flush(); output. close(); ///Read the key-value pair data in the file into the program, load() properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("entry.properties")); System.out.println(properties.getProperty("1"));//The properties file of the current thread InputStream input = new BufferedInputStream(new FileInputStream("D://2//1.properties")); properties. load(input); System.out.println(properties.getProperty("1"));