Map
Summary
1. Map: Translation is "Map"
Map: The correspondence between a point on paper and an actual location in real life
2. Map in java:
Correspondence between recording one data and another
3. Corresponding relationship:
Computational formulas and expressions
F(x) = x*x+1, where x belongs to [-1, 3] in positive integers
List the exhaustive ways:
{-1=2, 0=1, 1=2, 2=5, 3=10}
4. The second way to express correspondence in java. Maps also express correspondence in an exhaustive way. Dictionaries (by exhaustive way, the correspondence between words and explanations)
5. The characteristics of Map:
1. Corresponding relations are divided into "keys" and "values", where:
Keys, called key s, record simple, memorable information.
Values, called value s, record complex, ultimately obtainable, hard-to-remember information.
2. The key is unique, otherwise the same key will correspond to different values, thus losing the role of the corresponding relationship, resulting in ambiguity.
3. A key can only correspond to one value
4. Values can be repeated and multiple keys can correspond to the same value
Simple Method in Map
1. Description:
Map is an interface that defines what functions should be available in a two-column collection
2. Common functions:
put(K key, V value) If the key does not exist in the collection, add a pair of key-value pairs remove(Object key) Delete key-value pairs based on specified keys put(K key, V value) If the key already exists in the collection, then the corresponding value is modified. get(Object obj) Get the corresponding value according to the given key size() Get the logarithm of key-value pairs clear() Empty Set containsKey(Object obj) Determine whether obj keys exist in a set containsValue(Object obj) Determine whether there is a value in the set isEmpty() Judging whether a set is empty
Code examples
import java.util.HashMap; import java.util.Map; public class Demo04_Map { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("abc", 3); map.put("qq", 2); map.put("zxcv", 4); map.put("", 666); System.out.println(map); System.out.println(map.get("abc")); map.remove("abc"); System.out.println(map); map.put("", 0); System.out.println(map); System.out.println(map.isEmpty()); System.out.println(map.size()); System.out.println(map.containsKey("")); System.out.println(map.containsValue(666)); map.clear(); System.out.println(map); } }
Traversal of Map
1. Overview:
1. Can't traverse directly, can only get some information in Map, or do some transformation to Map, indirect traversal.
2. There are two ways to convert Map: to get all keys in Map, to get corresponding values according to keys; to get all key-value pair objects in Map, to get keys and values in each key-value pair object, respectively.
2. Two ideas, each corresponding to two traversal modes
1. Reason: Get all keys to form a Set set, and get all key-value pairs of objects to form a Set set.
2. Two ways: when traversing Set sets, you can use iterators or enhanced for
Map's first traversal idea: getting values based on keys
1. Steps:
1. Get Set of Sets for all keys
2. Use Set traversal to traverse Set sets and get each key (iterator, enhancement for)
3. Get the corresponding value according to the current key
2. The methods involved are as follows:
1,Set<K> keySet() 2,V get(K key)
Map's second traversal idea: Getting keys and values from keys to objects
1. Steps:
1. Get the Set set of all key-value pair objects
2. Traverse the Set set Set of key-value pairs to get each key-value pair object (iterator, enhancement for)
3. Obtain the key and value of the key-value pair object according to the method of key-value pair object.
2. Classes and methods involved
Key Value Pair Object: Implementing Class Object of Map.Entry Interface
1. entry: entry
2. Map.Entry records an object of the key and value of the key-value pair.
3. Map is an interface. Map.Entry is an internal interface of Map interface.
4. There are two methods: getKey gets the key and getValue() gets the value.
Secondly, after acquiring the key-value pair object, we no longer need to rely on the original Map set.
Code examples
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Demo05_Map The traversal of ___________ { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("abc", 3); map.put("qq", 2); map.put("zxcv", 4); map.put("", 666); test22_Getting keys and values from key-value pairs of objects_Enhance for(map); } private static void test22_Getting keys and values from key-value pairs of objects_Enhance for(Map<String, Integer> map) { for (Map.Entry<String, Integer> en : map.entrySet()) { System.out.println(en.getKey() + "..." + en.getValue()); } } private static void test21_Getting keys and values from key-value pairs of objects_iterator(Map<String, Integer> map) { //Get the Set set of all key-value pair objects Set<Map.Entry<String, Integer>> entrys = map.entrySet(); //iterator Iterator<Map.Entry<String, Integer>> it = entrys.iterator(); while (it.hasNext()) { //Get the next key-value pair object Map.Entry<String, Integer> en = it.next(); //Gets the key of the key to the object String key = en.getKey(); //Gets the value of the key to the object int value = en.getValue(); System.out.println(key + "..." + value); } } private static void test12_Get values based on keys_Enhance for(Map<String, Integer> map) { //Get values based on keys for (String key : map.keySet()) { System.out.println(key + "..." + map.get(key)); } } private static void test11_Get values based on keys_iterator(Map<String, Integer> map) { //Get the Set of Sets for all keys Set<String> keys = map.keySet(); //Traversing Set Sets Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key = it.next(); int value = map.get(key); System.out.println(key + "..." + value); } } }
Exercise: Keyboard input a string, count the number of occurrences of each character
Code examples
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Demo06_Statistical exercises { public static void main(String[] args) { // Keyboard enters a string and counts the number of occurrences of each character Scanner sc = new Scanner(System.in); String line = sc.nextLine(); char[] chs = line.toCharArray(); //Prepare a container for statistics, the correspondence between characters and numbers Map<Character, Integer> map = new HashMap<>(); for (char c : chs) { /*if (map.containsKey(c)) { int count = map.get(c); map.put(c, count + 1); } else { map.put(c, 1); }*/ map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1); } System.out.println(map); //The required printing results are as follows: a has 4, b has 4,.... There are two. System.out.println(getString(map)); } public static String getString(Map<Character, Integer> map) { StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> en : map.entrySet()) { char c = en.getKey(); int v = en.getValue(); sb.append(c).append("Yes").append(v).append("One,"); } sb.replace(sb.length() - 1, sb.length(), ". "); return sb.toString(); } }
HashMap
Summary
1. It is an implementation class of Map, which is implemented by hash storage.
2. View the source code of HashSet:
1. HashSet is actually HashMap at the bottom. HashSet is implemented by HashMap.
2. All features of HashSet elements can be ported to HashMap keys
3. HashSet guarantees the uniqueness of elements, the same as HashMap guarantees the uniqueness of keys.
4. Element types in HashSet need to override hashCode and equals methods, and key types in HashMap also need to be overridden.
3. Source screenshots:
LinkedHashMap
1. Keys can be stored and removed in the same order.
2. It is a subclass of HashMap and an implementation class of Map.
Code examples
import java.util.LinkedHashMap; public class Demo08_LinkedHashMap { public static void main(String[] args) { LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>(); lhm.put("asdf", 4); lhm.put("qq", 2); lhm.put("xyz", 3); System.out.println(lhm); } }
Comparison of HashMap and Hashtable
1. Similarities:
1. Both are implementation classes of Map
2. It's all hash storage
3. The method of membership is almost the same.
2. Differences:
Version 1: HashMap in jdk1.2, Hashtable in jdk1.0
2. Thread security is different:
HashMap threads are insecure and efficient
Hashtable threads are safe and inefficient
3. Storage of null is different
HashMap can store null key null values
Hashtable cannot store null key null values
4. Naming specifications are different:
The first letter of the second word Hashtable is not capitalized
Code examples
import java.util.HashMap; import java.util.Hashtable; public class Demo09_HashMap and Hashtable Comparison { public static void main(String[] args) { HashMap<String, String> hm = new HashMap<>(); hm.put(null, ""); hm.put("kong", null); hm.put(null, null); System.out.println(hm); } private static void test1_Hashtable Unable to store null key null value() { Hashtable<String, String> ht = new Hashtable<>(); // ht.put(null, ""); // ht.put("", null); // ht.put(null, null); System.out.println(ht); } }
So far, our collection has come to an end, and I will continue to update the summary of java knowledge.