Java HashMap

巧了我就是萌 提交于 2020-03-04 16:10:24

 

import java.util.Map;

/**
 * @author yuanxuan-chen
 * @date 2020-03-04 13:06
 */
public class NP_HashMap {
    public static void main(String[] args) {
        /**
         * Map, 键值对,key-value,
         */
        Map<Integer,Integer> map = new java.util.HashMap<Integer,Integer>();

        /**
         * 添加,键是唯一的
         */
        map.put(1, 3);
        map.put(1, 7);
        map.put(2, 6);
        map.put(3, 9);

        /**
         * 将map2,加到map后面
         */
        Map<Integer, Integer> map2 = new java.util.HashMap<>();
        map.put(4, 6);
        map.put(5, 6);
        map.put(6, 6);
        map.putAll(map2);

        /**
         * 输出键对应的值
         */
        int tmp = map.get(1);
        System.out.println(tmp);

        /**
         * map大小
         */
        int size = map.keySet().size();
        System.out.println(size);

        /**
         * map循环输出
         */
        System.out.print(map);

        for (Map.Entry<Integer, Integer> entry : map.entrySet() ) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

        for (Integer i : map.keySet() ) {
            System.out.println(i);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!