Java learning-Map, java learning map

Source: Internet
Author: User

Java learning-Map, java learning map
Preface

Recently, many cloud platform projects in colleges and universities are engaged in Map, and map usage is not very familiar. So I will study the map methods again here.



 

Map and Collection

 

When it comes to the Map set interface, we can't help but mention the Collection set interface. Both map and Collection are set interfaces. Collection contains the list and set subinterfaces that we often use; while Map is at the same level as Collection; Collection stores a group of objects, while Map stores a key-value Pair (key/value ).


 

Map

 

In a Map object, keys are unique and cannot be repeated. Null can also be used as a key, but such a key can only have one. However, the values corresponding to one or more keys are null.

 

When we want to determine whether a key exists in the map, we can use the containsKey () method to determine whether the key exists. We also need to use the containsValue () method to determine whether the value exists. The Code is as follows:

 

<pre name="code" class="java">public static void main(String[] args) {Map< Serializable, Serializable > map = new HashMap< Serializable, Serializable >();map.put(null,null);map.put("a", "1");map.put("b", "2");map.put("c", "3");if (map.containsKey("a")) {System.out.println("Key=Ture");if (map.containsValue("1")) {System.out.println("value=Ture");}}}


The execution result is: 

Key = Ture value = Ture

 

Map provides some common methods to retrieve the data in the Map, such as the entrySet () method. The returned value of entrySet () is a Set, the set type is Map. entry. Map. Entry is an internal interface declared by Map. This interface is generic and is defined as Entry <K, V>. It indicates an object in Map (a key-value Pair ). The Interface contains the getKey () and getValue methods. The Code is as follows:

 

public static void main(String[] args) {Map< Serializable, Serializable > map = new HashMap< Serializable, Serializable >();map.put(null,null);map.put("a", "1");map.put("b", "2");map.put("c", "3");Set<Entry<Serializable, Serializable>> entrySet= map.entrySet();System.out.println("entrySet="+entrySet);for (Entry key : entrySet) {System.out.println("key.getKey="+key.getKey()+" key.getValue()="+ key.getValue());}}

The execution result is 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. Then we can get the value through the get (key) traversal. The Code is as follows:


public static void main(String[] args) {Map< Serializable, Serializable > map = new HashMap< Serializable, Serializable >();map.put(null,null);map.put("a", "1");map.put("b", "2");map.put("c", "3");Set keySet= map.keySet();System.out.println("keySet="+keySet);for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {Object key = (Object) iterator.next();Object value = map.get(key);System.out.println("key = "+key+ " value="+value);}}

The execution result is as follows:


KeySet = [null, a, B, c]

Key = null value = null

Key = a value = 1

Key = B value = 2

Key = c value = 3

 

 

Finally, map has a values () method. The return value of the values () method is a set of values in Map. The value can be retrieved through traversal. The Code is as follows:


public static void main(String[] args) {Map<Serializable, Serializable> map = new HashMap<Serializable, Serializable>();map.put(null, null);map.put("a", "1");map.put("b", "2");map.put("c", "3");Collection c = map.values();System.out.println("map.values()=" + map.values());for (Iterator iterator = c.iterator(); iterator.hasNext();) {Object value = (Object) iterator.next();System.out.println("value="+value);}}

The code execution result is as follows:

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

Value = null

Value = 1

Value = 2

Value = 3

 


 

Summary

 

This article mainly introduces the use of the entrySet () method and the keySet () and value () methods in the Map set. The first two obtain the key ing relationship between key and value, only the final values extracts the value from the set and does not have a key, so there is no corresponding ing relationship.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.