【HashMap:根据键获取值,获取所有的键,获取所有的值】

package com.yjf.esupplier.common.test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author shusheng
 * @description 根据键获取值,获取所有的键,获取所有的值
 * @Email [email protected]
 * @date 2018/12/17 17:09
 */
public class MapDemo2 {

    public static void main(String[] args) {

        //创建集合
        Map<String, String> map = new HashMap<String, String>();

        //添加元素
        map.put("邓超", "孙俪");
        map.put("黄晓明", "杨颖");
        map.put("周杰伦", "昆凌");
        map.put("刘恺威", "杨幂");
        map.put("刘恺威", "杨幂1");

        //V get(Object key):根据键获取值
        System.out.println("get:" + map.get("周杰伦"));
        System.out.println("-------------------------");

        //Set<K> keyset():获取集合的所有键的集合
        Set<String> set = map.keySet();
        for (String key : set) {
            System.out.println(key);
        }
        System.out.println("-------------------------");

        //Collection<V> values():获取集合中所有值的集合
        Collection<String> values = map.values();
        for (String v : values) {
            System.out.println(v);
        }
        System.out.println("-------------------------");

        Set<Map.Entry<String,String>> entrySet = map.entrySet();
        for(Map.Entry<String,String> me:entrySet){
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key + "---" + value);
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/zuixinxian/p/10341170.html