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

How to get Map Collection in Java

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

Share

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

This article mainly introduces how to obtain the relevant knowledge of the Map collection in Java, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Java article on how to obtain the Map collection, let's take a look at it.

I. Overview

Interface Map k: type of key; V: type of value

Objects that map keys to values; cannot contain duplicate keys; each key can be mapped to up to one value

Second, the object method of creating Map collections

1. Using a polymorphic approach

two。 Concrete implementation class HashMap

Public static void main (String [] args) {/ / create Map collection object Map m=new HashMap (); / / add elements using put method, default natural sort m.put ("02", "Li Si"); m.put ("04", "Zhao Liu"); m.put ("01", "Zhang San"); m.put ("03", "Wang Wu") System.out.println (m);}}

III. Common methods of Map sets

Method name description V put (K key,V value) add element, adding duplicate key value element will override V remove (Object key) clear all key value pairs to element void clear () according to key deletion key value element Boolean containsKey (Object key) to determine whether the collection contains the specified key, including returning trueBoolean containsValue (Object value) to determine whether the set contains the specified value, including returning trueBoolean isEmpty () to determine whether the set is empty int size () to get the length of the set. That is, the number of key-value pairs public class MapDemo01 {public static void main (String [] args) {/ / create the Map collection object Map m=new HashMap () / / add elements: put method m.put ("1", "Zhang San"); m.put ("2", "Li Si"); m.put ("3", "Wang Wu"); m.put ("4", "Zhao Liu"); / / System.out.println (m) / / delete the key value pair element System.out.println (m.remove ("3")) according to the key; / / remember to write whatever type the key is, otherwise null System.out.println (m) will be returned; / / clear all key value pair element m.clear (); / / Boolean isEmpty () determine whether the set is empty System.out.println (m.isEmpty ()) / / System.out.println (m); / / Boolean containsKey (Object key); determines whether the set contains the specified key System.out.println (m.containsKey ("5")) / / write what the key is, otherwise it will return null / / Boolean containsValue (Object value) to determine whether the collection contains the specified value, including true System.out.println (m.containsValue ("Zhang San")); / / int size () to get the length of the set, that is, the number of key-value pairs System.out.println (m.size ()) Fourth, the method name of the Map acquisition method V get (Object key) gets the set of all keys based on the key acquisition value SetkeySet (). Gets the collection of all values SetentrySet () gets the collection of all key-value pairs of objects public class MapDemo02 {public static void main (String [] args) {/ / create Map object Map m=new HashMap () / / add elements m.put ("1", "Zhang San"); m.put ("3", "Li Si"); m.put ("4", "Wang Wu"); m.put ("2", "Zhao Liu"); / / System.out.println (m) / / V get (Object key) gets the value System.out.println (m.get ("3")) according to the key; / / pay attention to the type of key. If the type is not correct, it will report null / / SetkeySet () to get the collection of all keys. Because it is a collection, it traverses Setk = m.keySet () with enhanced for; for (String key:k) {System.out.println (key) } / / Collectionvalues () gets the collection of all values. Note that he sorts the values according to the order of the key: Collection c=m.values (); for (String vvirtual c) {System.out.println (v);}}. Fifth, the traversal mode of the Map collection:

1. First get the collection of all the keys in the Map collection, using the method setKey ()

two。 Traverse the set of all keys to get each key

3. GetValues method to get the corresponding value through each key

Public static void main (String [] args) {/ / create Map collection object Map m=new HashMap (); / / add key value pairs m.put ("1", "Zhang San"); m.put ("3", "Li Si"); m.put ("4", "Wang Wu"); m.put ("2", "Zhao Liu") / / get the set of all keys Sets=m.keySet (); / / traverse for (String key:s) {/ / and then get the corresponding value String value=m.get (key) through the key; System.out.println (key+ "," + value);}} method 2:

1. Get the collection of all key-value pairs, using the SetentrySet () method

two。 Iterate through this collection to get each key-value pair object, that is, the Map.Entry object.

3. Then get the value and key to the object according to the key value.

GetKey () get key

GetValue () gets the value

Public static void main (String [] args) {/ create Map collection object / / Map m=new HashMap (); / / add key-value pairs / / m.put ("1", "Zhang San"); / / m.put ("3", "Li Si"); / / m.put ("4", "Wang Wu") / / m.put ("2", "Zhao Liu"); / get the collection of all keys / / Sets=m.keySet (); / traverse / / for (String key:s) {/ get the corresponding value / / String value=m.get (key) through the key; / / System.out.println (key+ "," + value) / / Mode 2 / create Map collection object Map m=new HashMap (); / / add key value pairs m.put ("1", "Zhang San"); m.put ("3", "Li Si"); m.put ("4", "Wang Wu"); m.put ("2", "Zhao Liu") / / get the set of all key-value pairs SetentrySet () Set s = m.entrySet (); / / traverse the set for (Map.Entry ss:s) {/ / get the key value String key=ss.getKey () through the key-value pair object; / / get the value String value=ss.getValue () through the key-value pair object System.out.println (key+ "," + value);} this article on "how to get Map collections in Java" ends here. Thank you for reading! I believe you all have a certain understanding of "how to get Map collection in Java". If you want to learn more, you are welcome to follow the industry information channel.

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