Generally speaking, a Map is a data structure composed of key-value pairs, and each key is unique in the set. Let’s use K and V to represent keys and values to illustrate the nine major issues about Map in Java.
0. Convert Map to List type
In Java, Map interface provides three ways to obtain collections: Key set, value set, and key-value set. They can all be converted to List type by constructor or addAll() method. The following code shows how to construct an ArrayList from a Map:
// key listList keyList = new ArrayList(map.keySet());// value listList valueList = new ArrayList(map.valueSet());// key-value listList entryList = new ArrayList(map.entrySet());
1. Traverse the Map through Entry
This way in Java exists as key-value pairs is called Map.Entry. Map.entrySet() returns a key-value collection, which is a very efficient way to traverse.
for(Entry entry: map.entrySet()) {// get keyK key = entry.getKey();// get valueV value = entry.getValue();} Iterator is also used frequently, especially before JDK1.5
Iterator itr = map.entrySet().iterator(); while(itr.hasNext()) {Entry entry = itr.next();// get keyK key = entry.getKey();// get valueV value = entry.getValue();} 2. Sort Map by Key
Sorting requires frequent operations on Map's ke. One way is to implement it through a comparator:
List list = new ArrayList(map.entrySet());Collections.sort(list, new Comparator() {@Overridepublic int compare(Entry e1, Entry e2) {return e1.getKey().compareTo(e2.getKey());}}); Another method is to use SortedMap, but the Comparable interface must be implemented.
SortedMap sortedMap = new TreeMap(new Comparator() {@Overridepublic int compare(K k1, K k2) {return k1.compareTo(k2);}});sortedMap.putAll(map);3. Sort the Map with value <br />This is somewhat similar to the previous point, the code is as follows:
List list = new ArrayList(map.entrySet());Collections.sort(list, new Comparator() {@Overridepublic int compare(Entry e1, Entry e2) {return e1.getValue().compareTo(e2.getValue());}});4. Initialize a static constant Map
When you want to create a global static map, we have two ways, and it is thread-safe.
In Test1, although we declare that map is static, we can still change its value when initialized, just like Test1.map.put(3,"three");
In Test2, we set it to be non-modified through an inner class, so when we run Test2.map.put(3,"three"), it will throw a
UnsupportedOperationException exception prohibits you from modifying. public class Test1 {private static final Map map;static {map = new HashMap();map.put(1, "one");map.put(2, "two");}}public class Test2 {private static final Map map;static {Map aMap = new HashMap();aMap.put(1, "one");aMap.put(2, "two");map = Collections.unmodifiableMap(aMap);}} 5. The difference between HashMap, TreeMap, and Hashtable
In the Map interface, there are three implementations: HashMap, TreeMap, and Hashtable.
They are different. For details, please refer to the article "HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap".
6. Reverse query in Map
After we add a key-value pair to the Map, it means that in the Map, the key and the value correspond one by one, and a key corresponds to a value. But sometimes we need to reverse query, such as searching for its key through a certain value. This data structure is called a bididirectional map. Unfortunately, JDK does not support it.
Apache and Guava jointly provide this bidirectional map implementation, in which it stipulates that both keys and values must be 1:1 relationship.
7. Copying the Map
Java provides many methods that can realize the replication of a map, but those methods may not be synchronized at any time. Simply put, it is the change that occurs in a map, and the copied one remains the same. Here is a relatively efficient implementation method:
Map copiedMap = Collections.synchronizedMap(map);
Of course there is another method, which is cloning. But our java originator Josh Bloch does not recommend this method. He once said in an interview about Map cloning: cloning methods are provided in many classes because people do need them. However, cloning is very limited and often causes unnecessary impact. (Original text "Copy constructor versus cloning")
8. Create an empty map
If this map is set to unavailable, it can be implemented by the following
map = Collections.emptyMap();
On the contrary, when we can use it, we can
map = new HashMap();
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.