Sort by key: Sort by Comparator
Sort by Key is mainly used for TreeMap, which can be directly inserted into the appropriate position when the object is inserted according to the size of the Key value, and maintain the order of the map.
Let’s look at the constructor of TreeMap: TreeMap(Comparator<? super K> comparator): Constructs a new, empty tree map, which is sorted by a given comparator.
The comparator here is the key comparator. So the two parameters used for comparison when defining the comparator are the objects of the data type of Key.
Comparative Declarator:
public class MapKeyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
Sort using a declarer:
Map<String, Map<String, List<Insurce>>> sortMap = new TreeMap<>(new MapKeyComparator());
sortMap.putAll(collect);
System.out.println(sortMap);
Sort reference link:/hui008/article/details/81702883?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-81702883-blog-124148763.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-81702883-blog-124148763.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=1
Traversal: 4 ways to traverse map
1. Use entrySet traversal in the for loop, you can also get the key and value at the same time, and use the most
Map <String,String>map = new HashMap<String,String>();
map.put("Big Big", "brown");
map.put("Bear Two", "yellow");
for(Map.Entry<String, String> entry : map.entrySet()){
String mapKey = entry.getKey();
String mapValue = entry.getValue();
System.out.println(mapKey+":"+mapValue);
}
2. Use() traversal key in a for loop to get a single value through (key), or use() traversal to get values
This method is generally suitable for use when only the key or value in the map is needed, and it is better in performance than using entrySet;
Map <String,String>map = new HashMap<String,String>();
map.put("Big Big", "brown");
map.put("Bear Two", "yellow");
//key
for(String key : map.keySet()){
System.out.println(key);
}
//value
for(String value : map.values()){
System.out.println(value);
}
System.out.println("By traversing key and value");
for (String key:map.keySet()){
System.out.println("key= "+key+" and value= "+map.get(key));
}
3. Traversing through Iterator
When using Iterator traversal: When using foreach to traverse map, if it changes its size, an error will be reported, but if it is just deleted, you can use Iterator's remove method to delete the element
Iterator<Entry<String, String>> entries = map.entrySet().iterator();
//When using an iterator, note that the next() method cannot appear twice in the same loop!
// Otherwise, a NoSuchElementException will be thrown.
while(entries.hasNext()){
Entry<String, String> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
4. Find value traversal through keys (that is the second way to traverse key + get value)
This method is relatively inefficient because it is a time-consuming operation to draw values from keys and is not recommended;
for(String key : map.keySet()){
String value = map.get(key);
System.out.println(key+":"+value);
}
Some conclusions:
If you just get the key or value, it is recommended to use keySet or values
If you need both key and value, it is recommended to use entrySet
Iterator is recommended if you need to delete elements during traversal
If you need to add elements during the traversal process, you can create a new temporary map to store the newly added elements. After the traversal is completed, then put the temporary map into the original map.
Map traversal reference link:/qq_44750696/article/details/112254719