Interface Map k
: key type; V: value type
will Object that maps keys to values; cannot contain duplicate keys; each key can be mapped to at most one value
1. Use polymorphic method
2. Specific implementation class HashMap
public static void main(String[] args) { //创建Map集合对象 Map<String,String> m=new HashMap<String,String>(); //添加元素使用put方法,默认自然排序 m.put("02","李四"); m.put("04","赵六"); m.put("01","张三"); m.put("03","王五"); System.out.println(m); } }
3. Common methods of Map collection
Method name | Description |
---|---|
V put(K key,V value) | Add elements, adding duplicate key-value elements will overwrite |
V remove(Object key) | Delete key-value pair elements based on key |
void clear() | Clear all key-value pair elements |
Boolean containsKey(Object key) | Determine whether the collection contains the specified Key, contains returns true |
Boolean containsValue(Object value) | Determines whether the collection contains the specified value, contains returns true |
Boolean isEmpty() | Determine whether the set is empty |
int size() | Get the length of the set, that is, the key-value pair Number |
public class MapDemo01 { public static void main(String[] args) { //创建Map集合对象 Map<String,String> m=new HashMap<String,String>(); //添加元素,put方法 m.put("1","张三"); m.put("2","李四"); m.put("3","王五"); m.put("4","赵六"); // System.out.println(m); //根据键删除键值对元素 System.out.println(m.remove("3"));//切记键是什么类型就写什么类型,不然会返回null System.out.println(m); //清除所有键值对元素 m.clear(); //Boolean isEmpty()判断集合是否为空 System.out.println(m.isEmpty()); // System.out.println(m); //Boolean containsKey(Object key);判断集合中是否包含指定的键 System.out.println(m.containsKey("5"));//切记键是什么类型就写什么类型,不然会返回null //Boolean containsValue(Object value)判断集合是否包含指定的值,包含返回true System.out.println(m.containsValue("张三")); //int size()获取集合的长度,也就是键值对的个数 System.out.println(m.size()); } }
Method name | Description |
---|---|
V get(Object key) | Get the value based on the key |
Get the collection of all keys | |
Get the collection of all values | |
Get the collection of all key-value pair objects |
Method
public static void main(String[] args) { //方式一 //创建Map集合对象 Map<String,String> m=new HashMap<String,String>(); //添加键值对 m.put("1","张三"); m.put("3","李四"); m.put("4","王五"); m.put("2","赵六"); //获取所有键的集合 Set<String>s=m.keySet(); //遍历 for (String key:s){ //再通过键获取相对应的值 String value=m.get(key); System.out.println(key+","+value); } } }
getValue()
Gets the valuepublic static void main(String[] args) { // //方式一 // //创建Map集合对象 // Map<String,String> m=new HashMap<String,String>(); // //添加键值对 // m.put("1","张三"); // m.put("3","李四"); // m.put("4","王五"); // m.put("2","赵六"); // //获取所有键的集合 // Set<String>s=m.keySet(); // //遍历 // for (String key:s){ // //再通过键获取相对应的值 // String value=m.get(key); // System.out.println(key+","+value); // } //方式二 //创建Map集合对象 Map<String,String> m=new HashMap<String,String>(); //添加键值对 m.put("1","张三"); m.put("3","李四"); m.put("4","王五"); m.put("2","赵六"); //获取所有键值对的集合Set<Map.Entry<K,V>>entrySet() Set<Map.Entry<String,String>> s= m.entrySet(); //遍历该集合 for (Map.Entry<String,String> ss:s){ //通过键值对对象获取键值 String key=ss.getKey(); //通过键值对对象获取值 String value=ss.getValue(); System.out.println(key+","+value); } } }
The above is the detailed content of How to get Map collection in Java. For more information, please follow other related articles on the PHP Chinese website!