3.get(Object key) : Get the value (value) corresponding to the specified key (key)
-
Returns null if the key does not exist in the collection
-
remove(Object key): delete the element according to the specified key (key) and return the value of the deleted element (value)
-
Returns null if the key does not exist in the collection
3. Map collection traversal - keySet method
-
keySet method: store all keys in the Set collection
-
Traversal steps:
-
Call the method keySet method of the map collection to store all the keys in the Set collection
-
Traverse the Set collection to get all the elements in the Set collection (keys in the Map)
-
Call the get method of the map collection to get the value through the key
public class MapDemo { public static void main(String[] args) { Map<String,Integer> map = new HashMap<String,Integer>(); map.put("a", 11); map.put("b", 12); map.put("c", 13); map.put("d", 14); Set<String> set = map.keySet(); Iterator<String> it = set.iterator(); while(it.hasNext()){ String key = it.next(); Integer value = map.get(key); System.out.println(key+"...."+value); } //Use enhanced for traversal for(String key : map.keySet()){ Integer value = map.get(key); System.out.println(key+"...."+value); }
4. Entry interface
- When the Map class is designed, a nested interface is provided: Entry. Entry encapsulates the corresponding relationship of key-value pairs into objects. That is, the key-value pair object, so that when we traverse the Map collection, we can get the corresponding key and corresponding value from each key-value pair (Entry) object
- Entry is a static inner nested interface provided in the Map interface
- Map.Entry(K, V)
- entrySet() method: used to return all the key-value pair (Entry) objects in the Map collection in the form of Set collection.
5. Map collection traversal - Entry
-
Key-value pair method: That is, through each key-value pair (Entry) object in the collection, the key and value in the key-value pair (Entry) object are obtained.
-
Traversal steps:
-
Call the map collection method entrySet() to store the mapping relationship objects in the collection to the Set collection
-
Traverse the Set collection containing key-value pair (Entry) objects to get each key-value pair (Entry) object
-
Through the key-value pair (Entry) object, use getKey, getValue to obtain the key and value in the Entry object.
Map collections cannot be traversed directly using iterators or enhanced for. But it can be used after converting to Set.
public class MapSet { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>(); map.put(1,"a"); map.put(2,"ab"); map.put(3,"abc"); //Call the map collection method entrySet() to store the mapping relationship objects in the collection to the Set collection Set<Map.Entry<Integer,String>> set = map.entrySet(); //Traverse the Set collection Iterator<Map.Entry<Integer,String>> it = set.iterator(); while(it.hasNext()){ //Get the elements in the Set collection, that is, the mapping relationship object //it.next gets the Map.Entry object Map.Entry<Integer,String> entry = it.next(); //Use getKey, getValue to get the key and value in the Entry object through the key-value pair (Entry) object Integer key = entry.getKey(); String value = entry.getValue(); System.out.println(key+" : "+value); } } //Enhanced for loop for(Map.Entry<Integer,String> entry : map.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); } }
6. HashMap stores custom type key values
- When storing a custom object in HashMap, if the custom object exists as a key, then to ensure that the object is unique, the hashCode and equals methods of the object must be overridden
- If you want to ensure that the key s stored in the map are in the same order as they are retrieved, you can use the LinkedHashMap collection to store them.
2. Object-Oriented Thought of Set Inheritance System
- Interface: used to clarify the functions that all sets should have, which is equivalent to defining set function standards;
- Abstract class: Extract the methods with the same function implementation in multiple collections to the abstract class implementation, and the specific collections are no longer written, but can be inherited and used;
- Concrete classes: inherit abstract classes, implement interfaces, and override all abstract methods to achieve a collection with specified functions. Each specific collection class implements the functional methods in the interface in different ways according to its own data storage structure.
Interview preparation + review material sharing:
In order to cope with the interview, I also brushed a lot of interview questions and materials. Now I will share it with readers and friends who need it. I only cut out a part of the information. If you need it, you can come to me to get it.
How to get it: Click on the blue font to get it for free
you can come to me to get it
How to get it: Click on the blue font to get it for free
[External link image dumping...(img-5nM1sEKE-1628408390779)]