Java map learning and code examples

preface

Recently, in the University cloud platform project being done, I have more contact with map, and I am not very familiar with the use of map, so I will learn several methods of map again.

Map and collection

When we mention the map collection interface, we cannot fail to mention the collection interface. Both map and collection are collection interfaces. The collection contains the list and set sub interfaces we often use; Map is at the same level as collection; A collection stores a set of objects, while a map stores a key / value pair

Map

Java defines an interface for mapping in data structures util. Map map is mainly used to store key value pairs and obtain values according to keys. Therefore, duplicate keys (repeated and overwritten) are not allowed, but duplicate values are allowed.

Map provides a more general element storage method. The map collection class is used to store element pairs (called keys and values), where each key is mapped to a value. Conceptually, you can think of list as a map with numeric keys. In fact, there is no direct connection between list and map except that both list and map define java.util.

In the map object, the key is unique and cannot be repeated. Null can also be used as a key, but there can only be one such key; However, one or more keys can have null values.

Common API:

When we want to judge whether there is a key in the map, we can use the method containskey(). Similarly, when we want to judge whether there is a value, we can use the method containsvalue(); The code is as follows:

The execution result is: key = ture value = ture

Map provides some common methods to retrieve the data in the map, such as entryset () method,; The return value of entryset() is a set set of type map Entry。 Map. Entry is an internal interface declared by map. This interface is generic and is defined as entry < K, V >. It represents an entity (a key value pair) in the map. There is a getkey() getValue method in the interface. The code is as follows:

The results of the implementation are as follows:

entrySet=[null=null,a=1,b=2,c=3] key. getKey=null key. getValue()=null key. getKey=a key. getValue()=1 key. getKey=b key. getValue()=2 key. getKey=c key. getValue()=3

Next, we will talk about the keyset method. The return value of the keyset () method is the set of key values in the map, and then you can get the value value through get (key) traversal. The code is as follows:

The results of the implementation are as follows:

keySet=[null,a,b,c] key = null value=null key = a value=1 key = b value=2 key = c value=3

Finally, the map also has a values () method. The return value of the values () method is the collection of value values in the map. The value can be retrieved by traversal. The code is as follows:

The code execution results are as follows:

map. values()=[null,1,2,3] value=null value=1 value=2 value=3

A self-made small example of HQL statement splicing using map:

summary

This paper mainly introduces the use of entryset() method and keyset() and value() methods in the map set. The first two methods take out the mapping relationship between key and value. Only the last values take out all the values in the set. If there is no key, there will be no corresponding mapping relationship.

The above is all about the learning of Java map and code examples in this article. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Code examples of four traversal methods of map collection

Java map stores the array and takes out the value code

On the mutual transformation between object and map

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>