Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the nine questions about Map in JAVA?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about what are the nine major issues about Map in JAVA. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Generally speaking, Map is a data structure made up of key-value pairs, and each key in the collection is *. Let's use K and V to represent keys and values to illustrate nine major problems about Map in java.

0. Convert Map to List type

In java, the Map interface provides three ways to get collections: Key set,value set, and key-value set. They can all be converted to List types through constructors or addAll () methods. The following code shows how to construct an ArrayList from Map:

/ / key list

List keyList = new ArrayList (map.keySet ())

/ / value list

List valueList = new ArrayList (map.valueSet ())

/ / key-value list

List entryList = new ArrayList (map.entrySet ())

1. Traverse Map through Entry

This way of existing key-value pairs in java is called Map.Entry. Map.entrySet () returns a collection of key-value, which is a very efficient way to traverse.

For (Entry entry: map.entrySet ()) {

/ / get key

K key = entry.getKey ()

/ / get value

V value = entry.getValue ()

}

We also often use Iterator, especially before JDK1.5.

Iterator itr = map.entrySet () .iterator ()

While (itr.hasNext ()) {

Entry entry = itr.next ()

/ / get key

K key = entry.getKey ()

/ / get value

V value = entry.getValue ()

}

2. Sort the Map through Key

Sorting requires frequent operations on the ke of Map, and one way is through the comparator:

List list = new ArrayList (map.entrySet ())

Collections.sort (list, new Comparator () {

@ Override

Public int compare (Entry E1, Entry e2) {

Return e1.getKey () .compareTo (e2.getKey ()

}

});

Another way is through SortedMap, but you must implement the Comparable interface.

SortedMap sortedMap = new TreeMap (new Comparator () {

@ Override

Public int compare (KK1, KK2) {

Return k1.compareTo (K2)

}

});

SortedMap.putAll (map)

3. Sort value and Map

This is somewhat similar to the previous point, with the following code:

List list = new ArrayList (map.entrySet ())

Collections.sort (list, new Comparator () {

@ Override

Public int compare (Entry E1, Entry e2) {

Return e1.getValue () .compareTo (e2.getValue ()

}

});

4. Initialize a constant Map of static

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 during initialization, like Test1.map.put (3, "three").

In Test2, we make it unmodifiable through an inner class, so when we run Test2.map.put (3, "three"), it throws a UnsupportedOperationException exception to prevent you from modifying it.

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 and Hashtable

In the Map interface, there are three implementations: HashMap,TreeMap,Hashtable.

There are differences between them. For details, please refer to the article "HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap".

6. Reverse query in Map

When we add a key-value pair to Map, it means that there is an one-to-one correspondence between key and value in Map, and a key corresponds to a value. But sometimes we need a reverse query, such as finding its key by a value, a data structure called bidirectional map, which is unfortunately not supported by JDK.

Apache and Guava together provide this bidirectional map implementation, which specifies that both the key and value must be 1:1.

7. Replication of Map

There are many ways to copy a Map in java, but those methods do not necessarily synchronize all the time. To put it simply, it is a change that happens to a Map, while the copied one remains the same. Here is a more efficient implementation:

Map copiedMap = Collections.synchronizedMap (map)

Of course, there is another way, and that is cloning. But Josh Bloch, our ancestor of java, does not recommend this approach. He once talked about Map cloning in an interview: cloning is available in many classes because people really need it. But cloning is very limited and in many cases has an unnecessary impact. (original text "Copy constructor versus cloning")

8. Create an empty Map

If the map is made unavailable, it can be implemented through the following implementation

Map = Collections.emptyMap ()

On the contrary, when we will use it, we can directly

Map = new HashMap ()

After reading the above, do you have any further understanding of the nine major questions about Map in JAVA? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report