导航

HashMap的常用方法

Posted on 2020-08-10 14:26  玻璃星  阅读(137)  评论(0编辑  收藏  举报
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

public class HasMapDemo {
    public static void main(String[] args) {
        HashMap<Integer,String> map = new HashMap<Integer,String>();
        //添加元素
        map.put(001, "诸葛亮");
        map.put(002, "刘备");
        map.put(003, "张飞");
        //删除指定key值的元素
        map.remove(002);
        //得到指定key值得value
        System.out.println(map.get(001));
        //得到key值集
        Set<Integer> set = map.keySet();
        //得到value集
        Collection<String> c=map.values();

        //遍历map
        for(Integer i:map.keySet()) {
            System.out.println(map.get(i));
        }
    }
}