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.
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
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")); } }
Run results
## 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); } } }
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); } } }
## 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): } } }
Running results
Get 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); } } }
Running results
##Properties collection
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); } } }
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!