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

Case Analysis of Java Map Collection usage

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Java Map collection use case analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java Map collection use case analysis" bar!

Map interface

The Map interface is a two-column collection, and each element of it contains a key object key and a value object Value. There is a corresponding relationship between the key object and the value object, called mapping. When accessing elements from the Map collection, as long as you specify Key, you can find the corresponding Value.

Common methods of Map

Void put (Object key,Object value) / / Associates the specified value with the specified key in the map

Object get (Object key) / / returns the value mapped by the specified key, or null if the mapping does not contain the mapping of the key

Boolean containsKey (Object key) / / returns true if this mapping contains a mapping relationship for the specified key

Boolean containsValue (Object value) / / returns true if the mapping maps one or more keys to the specified value at this time

Set keySet () / / returns the Set view of the values contained in this mapping

Collectionvalues () / / returns the Collection view of the values contained in this mapping

SetentrySet () / / returns a Set view of the mapping relationships contained in this mapping

HashMap collection

The HashMap collection is an implementation class of the Map interface, which is used to store key-value mappings, but must ensure that there are no duplicate keys.

Package collection class; import java.util.HashMap; import java.util.Map; public class Long {public static void main (String [] args) {Map map=new HashMap (); map.put ("1", "lilong"); map.put ("2", "xiaolong"); map.put ("3", "dalong"); System.out.println ("1:" + map.get ("1")) System.out.println ("2:" + map.get ("2"); System.out.println ("3:" + map.get ("3"));}}

Running result

First, three elements are added to the collection through Map's put method, and then the value corresponding to the key is obtained by Map's get method. If the same key is stored, the stored value will overwrite the original value, that is, the key is the same, and the value is overwritten.

First iterate through all the keys in the Map collection, and then get the corresponding values based on the keys (below)

Package collection class; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Li {public static void main (String [] args) {Map map=new HashMap (); map.put ("1", "lilong"); map.put ("2", "xiaolong"); map.put ("3", "dalong"); Set keySet= (Set) map.keySet (); Iterator it= keySet.iterator While (it.hasNext ()) {Object key=it.next (); Object value=map.get (key); System.out.println (key+ ":" + value);}

Running result

First, the Map is traversed by the hasnext () method, and the keySet () method of the Map object is called to get the Set collection that stores all the keys in the Map. Then each element of the Set set is iterated through Iterator, and each key gets the corresponding value through the get method.

The Map collection can first get all the mapping relationships in the collection, and then extract values and keys from the mapping relationships.

Package collection class; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Xiao {public static void main (String [] args) {Map map=new HashMap (); map.put ("1", "lilong"); map.put ("2", "xiaolong"); map.put ("3", "dalong"); Set entrySet= (Set) map.entrySet (); Iterator it= entrySet.iterator While (it.hasNext ()) {Map.Entry entry= (Map.Entry) (it.next ()); Object key=entry.getKey (); Object value=entry.getValue (); System.out.println (key+ ":" + value);}

Running result

First, call the entrySet () method of the Map object to get the Set collection of all mappings stored in Map, which stores elements of type Map.Entry, each Map.Entry object represents a key-value pair in Map, then iterates the Set set to get each mapping object, and calls the getKey () method and getValue () method of the mapping object to get the key and value respectively.

The Map collection also provides a values () method that allows you to directly get the Collection collection that stores all the values in the Map.

Package collection class; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class LiXiao {public static void main (String [] args) {Map map=new HashMap (); map.put ("1", "lilong"); map.put ("2", "xiaolong"); map.put ("3", "dalong"); Collection values=map.values (); Iterator it=values.iterator () While (it.hasnext ()) {Object value=it.next (); System.out.println (value):}

Running result

Get the Collection collection containing all the values in the Map by calling the value () method of Map, and then iterate over each value in the collection.

The order of the elements iterated out of the HashMap collection is inconsistent with the order of storage. To make the two orders consistent, you can use the LinkdedHashMap class provided in Java, which can use a two-way linked list to maintain the relationship between internal elements, so that the iterative order of Map elements is consistent with the order of storage.

Import java.util.Map; public class Long {public static void main (String [] args) {Map map=new LinkedHashMap (); map.put ("1", "lilong"); map.put ("2", "xiaolong"); map.put ("3", "dalong"); Set keySet= (Set) map.keySet (); Iterator it= keySet.iterator While (it.hasNext ()) {Object key=it.next (); Object value=map.get (key); System.out.println (key+ ":" + value);}

Running result

Properties collection

Properties is mainly used to store keys and values of string types, and you can use the Properties collection to access the application's configuration items.

Package collection class; import java.util.Enumeration; import java.util.Properties; public class Xiao {public static void main (String [] args) {Properties p=new Properties (); p.setProperty ("Backgroup-color", "red"); p.setProperty ("Font-size", "14px"); p.setProperty ("Language", "chinese"); Enumeration names=p.propertyNames () While (names.hasMoreElements ()) {String key= (String) names.nextElement (); String value=p.getProperty (key); System.out.println (key+ "=" + value);}

Running result

In the Properties class, two specialized methods, setProperty () and getProperty (), are provided for string access. The setProperty () method is used to add the values and keys of the configuration item to the Properties collection. Then get an Enumeration object that contains all the keys by calling the propertyNames () method of Properties, and then get the corresponding value of the key by calling the getProperty () method when traversing all the keys.

Thank you for your reading, the above is the content of "Java Map collection use case analysis", after the study of this article, I believe you have a deeper understanding of the problem of Java Map collection use case analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 232

*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