Home  >  Article  >  Java  >  How to get Map collection in Java

How to get Map collection in Java

王林
王林forward
2023-04-19 15:01:081189browse

1. Overview

  • 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

2. Object method of creating Map collection

  • 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());
    }
}

4. Map acquisition method

##Set245c3adc26563b673f7297c0b3777639keySet()Get the collection of all keysCollectiond94943c0b4933ad8cac500132f64757fvalues()Get the collection of all valuesSet5820729628dc9dadd6d3eda5ccf37a32>entrySet()Get the collection of all key-value pair objects##
public class MapDemo02 {
    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","赵六");
//        System.out.println(m);
        //V get(Object key)根据键获取值
        System.out.println(m.get("3"));//要注意键的类型,类型不对会报null
        //Set<K>keySet()获取所有键的集合,因为返回的是个集合,所以用增强for遍历
        Set<String> k=m.keySet();
        for (String key:k){
            System.out.println(key);
        }
        //Collection<V>values()获取所有值的集合,注意,他会按照键的排序对值进行排序
        Collection<String> c=m.values();
        for (String v:c){
            System.out.println(v);
        }

    }
}
5. Traversal method of Map collection
Method name Description
V get(Object key) Get the value based on the key

Method 1:

    1. First get the set of all keys in the Map collection and use the method
  • setKey()

  • 2. Traverse the set of all keys to get each key
  • 3. Get the corresponding value through each key
  • getValues

    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);
            }
        }
    }
  • Method 2:

    1. Get the collection of all key-value pairs, use Set44bf986331d5d9c3931140ad55669b0c> entrySet() method
  • 2. Traverse this set to obtain each key-value pair object, which is the Map.Entry object
  • 3. Then according to Key-value pair object gets the value and key
  • ##getKey()
Gets the key

getValue()

Gets the value

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);
//        }
        //方式二
        //创建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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete