Home > Java > javaTutorial > body text

Java Map collection usage example analysis

WBOY
Release: 2023-04-18 23:52:13
forward
906 people have browsed it

Map interface

The Map interface is a two-column collection. Each element of it contains a key object key and a value object Value. There is a correspondence between the key object and the value object, which is called mapping. When accessing elements from the Map collection, as long as the Key is specified, the corresponding Value can be found.

Common methods of Map

void put(Object key,Object value)//Associate the specified value with the specified key in the map
Object get(Object key) //Returns the value mapped to the specified key. If this map does not contain the mapping relationship of the key, returns null
boolean containsKey(Object key)//If this map contains the mapping relationship of the specified key, returns true
boolean containsValue(Object value)//If the mapping maps one or more keys to the specified value at this time, return true
Set keySet()//Return the Set view of the values ​​contained in this mapping
Collection< ;V>values()//Returns a Collection view of the values ​​contained in this map
Set>entrySet()//Returns a Set view of the mapping relationships contained in this map

HashMap collection

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

 package 集合类;
 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"));
     }
 }
Copy after login

Run results

Java Map collection usage example analysis

## First add 3 elements to the collection through the put method of Map, and then obtain the value corresponding to the key through the get method of Map , if the same key is stored, the value stored later will overwrite the original value, that is, if the key is the same, the value will be overwritten.

First traverse all the keys in the Map collection, and then obtain the corresponding values ​​​​according to the keys (as follows)

 package 集合类;
 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);
         }
     }
 }
Copy after login

Running results

Java Map collection usage example analysis

First Traverse the Map through the hasnext() method, call the keySet() method of the Map object, and obtain the Set collection that stores all the keys in the Map. Then iterate each element of the Set collection through the Iterator, and each key obtains the corresponding value through the get method.

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

 package 集合类;
 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);
         }
     }
 }
Copy after login

Run results

Java Map collection usage example analysis## First call the entrySet() method of the Map object to obtain the Set collection of all mappings stored in the Map. This collection stores the Map .Entry type element. Each Map.Entry object represents a key-value pair in the Map. Then iterate the Set collection to obtain each mapping object, and call the getKey() method and getValue() method of the mapping object to obtain the key and value. value.

The Map collection also provides a values() method, through which you can directly obtain the Collection that stores all values ​​in the Map.

 package 集合类;
 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):
         }
     }
 }
Copy after login

Running results

Java Map collection usage example analysisGet a Collection containing all the values ​​in the Map by calling the value() method of the Map, and then iterate out each item in the collection value.

The order in which the elements are iterated out of the HashMap collection is inconsistent with the order in which they are stored. To make the two orders consistent, you can use the LinkdedHashMap class provided in Java, which can use a doubly linked list to maintain the relationship between internal elements. , so that the order of Map element iteration 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);
         }
     }
 }
Copy after login

Running results

##Properties collectionJava Map collection usage example analysis

Properties mainly stores string type keys and values. You can use the Properties collection to store Get the application configuration items.

 package 集合类;
 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);
         }
     }
 }
Copy after login
Run results

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

The above is the detailed content of Java Map collection usage example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template