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 is the secret of the Map collection of Java things?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what are the unknown secrets of the Map collection of Java things?" friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the unknown secrets of the Map collection of Java things?"

1. Map

The characteristics of Map sets

-key-value pair mapping relationship

-A key corresponds to a value

-the key cannot be repeated and the value can be repeated

-element access disorder

We understand through one of his implementation classes, the HashMap collection.

Second, the basic concept of HashMap

Definition of HashMap:

HashMap is a hash table that stores key-value pair (key-value) mappings.

HashMap implements the Map interface, stores data according to the HashCode value of the key, has a fast access speed, allows at most one record key to be null, and does not support thread synchronization.

The HashMap is unordered, that is, the order in which it is inserted is not recorded.

HashMap inherits from AbstractMap and implements Map, Cloneable, and java.io.Serializable interfaces.

Then the key and value types of HashMap can be String types or wrapper class types for other data. When it comes to wrapper class types, by the way, let's review the wrapper class types:

III. The basic method and use of HashMap collection

After learning the basic concepts of the HashMap collection, how can we use it in the actual development process? We're going to use his unique method here.

The following are common methods for HashMap collections

It is important to note that the addition of elements to the Map collection is not the add () method of the Collection collection, but the Put () method. Because the Map collection is a collection of key-value pairs, how do we get every element in the collection? The collection here also provides us with his element acquisition method:

The sample code is as follows:

Import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Set; / * Map collection acquisition function: v get (Object key): get the value according to the key Set keySet (): get the collection of all keys Collection values (): get the collection of all values * / public class MapDemo03 {public static void main (String [] args) {Map m = new HashMap () M.put ("Zhang San", "Li Si"); m.put ("Wang Wu", "Zhao Liu"); m.put ("Boss Li", "skinhead Qiang"); / / V get (Object key): System.out.println (m.get ("Zhang San")); System.out.println (m.get ("Zhang Si")) System.out.println ("="); / / Set keySet (): get the collection of all keys Set s = m.keySet (); for (String I: s) {System.out.println (I);} System.out.println ("=") / / Collection values (): get the collection of all values Collection c = m.values (); for (String x: C) {System.out.println (x);}

Output result:

Li Si

Null

=

Zhang San

Wang Wu

Boss Li

=

Li Si

Zhao Liu

Bald strength

IV. Traversal of HashMap sets

In fact, traversal is nothing more than taking out the elements in the set one by one, and we can use it, so in the third part, we have already talked about a traversal way, that is, we get the collection of keys, and then use the loop to get the corresponding value of each key, so we take out every element in the set.

Traversal mode 1: find the value according to the set of keys

Step analysis

-gets a collection of all keys. Using keySet () method to realize

-iterate through the collection of keys to get each key. Implement with enhanced for

-find the value according to the key. Using get (Object key) method to realize

Code implementation

Traversal of the import java.util.Map;import java.util.HashMap;import java.util.Set; / * Map collection (mode 1): 1: get the collection of all keys. Use the keySet () method to implement 2: traverse the collection of keys and get each key. Implement 3 with enhanced for: find the value according to the key. Use get (Object key) method to implement * / public class MapDemo01 {public static void main (String [] args) {/ / create collection object Map map = new HashMap (); / / add elements map.put ("Zhang Wuji", "Zhao Min"); map.put ("Guo Jing", "Huang Rong"); map.put ("Yang Guo", "Xiao Longnu") / / 1: gets a collection of all keys. Use keySet () method to realize Set keySet = map.keySet (); for (String key: keySet) {String value = map.get (key); System.out.println (key + "," + value);}

Output result:

Yang Guo, Xiao Longnu

Guo Jing, Huang Rong

Zhang Wuji, Zhao Min

So the second method is that we use the entrySet () method to get all the key-value pairs in the set, and then get the right key or value.

HashMap collection traversal mode 2: key-value pair set finding key-value pair

Step analysis

-gets a collection of all key-value pairs of objects

-Set entrySet (): gets a collection of all key-value pairs of objects

-iterate through the collection of key-value pairs of objects to get each key-value pair object

-implemented with enhanced for to get each Map.Entry

-get keys and values for objects based on key values

-get the key with getKey ()

-get the value with getValue ()

Code implementation

Import java.util.HashMap;import java.util.Map;import java.util.Set / * traversal of the Map collection (mode 2): 1: get the collection of all key-value pairs objects Set entrySet (): get the collection of all key-value pairs objects 2: traverse the collection of key-value pairs objects, and get each key-value pair object implemented with enhanced for Get each Map.Entry 3: use getKey () to get the key and value for the object according to the key value, use getValue () to get the value * / public class MapDemo02 {public static void main (String [] args) {/ / create the collection object Map map = new HashMap () / / add elements map.put ("Zhang Wuji", "Zhao Min"); map.put ("Guo Jing", "Huang Rong"); map.put ("Yang Guo", "Xiao Longnu"); / / 1: get the collection of all key value pairs Set me = map.entrySet () / / iterate through the collection of key-value pair objects, and get each key-value pair object for (Map.Entry e: me) {String key = e.getKey (); String value = e.getValue (); System.out.println (key + "," + value);}

Output result:

Yang Guo, Xiao Longnu

Guo Jing, Huang Rong

Zhang Wuji, Zhao Min

Both traversal methods are very practical, and you can use whatever you like in the development.

5. A comprehensive case of HashMap set

Youdao is: rather than envy the fish in Linyuan, it is better to retreat and form a net. After seeing so many theoretical things, if you don't practice it yourself, then the fish will always be in the fish pond and the knowledge will always be in the computer. Then let's do a problem together to deepen our impression.

Case requirements:

Case requirement

-the keyboard enters a string that requires statistics of the number of occurrences of each string in the string.

-example: keyboard input "aababcabcdabcde" in the console output: "a (5) b (4) c (3) d (2) e (1)"

Train of thought analysis:

1: the keyboard enters a string

2: create a HashMap collection with a key of Character and a value of Integer

3: traverse the string to get each character

4: take each character you get as a key to the HashMap collection to find the corresponding value and see the return value

If the return value is null: indicates that the character does not exist in the HashMap collection, the character is stored as the key and 1 as the value

If the returned value is not null: indicates that the character exists in the HashMap collection, add the value by 1, and then re-store the character and the corresponding value

5: traverse the HashMap collection, get the keys and values, and splice them according to the requirements

6: output result

Code example:

Import java.util.HashMap;import java.util.Scanner;import java.util.Set; / * requirements: the keyboard enters a string that requires statistics of the number of occurrences of each string in the string. For example: keyboard input "aababcabcdabcde" in the console output: "a (5) b (4) c (3) d (2) e (1)" idea: 1: keyboard input a string 2: create a HashMap set, the key is Character, the value is Integer 3: traversal string, get each character 4: take each character as a key to find the corresponding value in the HashMap collection Look at the return value if the return value is null: indicates that the character does not exist in the HashMap collection, use the character as the key, and 1 as the value storage. If the returned value is not null: it indicates that the character exists in the HashMap collection, add the value 1, and then re-store the character and the corresponding value 5: traverse the HashMap collection to get the key and value Stitching as required 6: output result * / public class HashMapDemo {public static void main (String [] args) {Scanner sc = new Scanner (System.in) System.out.println ("Please enter a string:"); String s = sc.nextLine (); HashMap hm = new HashMap (); for (int I = 0; I < s.length (); istring +) {char key = s.charAt (I); Integer value = hm.get (key) If (value = = null) {hm.put (key, 1);} else {value++; hm.put (key, value);}} StringBuilder sb = new StringBuilder (); Set keySet = hm.keySet () For (Character key1: keySet) {Integer values = hm.get (key1); sb.append (key1) .append ("(") .append (values) .append (")");} String result = sb.toString (); System.out.println (result);}}

Output result:

Please enter a string:

Aababcabcdabcde

A (5) b (4) c (3) d (2) e (1)

At this point, I believe you have a deeper understanding of "what unknown secrets are there in the Map collection of Java things?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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