HashMap集合常见方法

一、HashMap集合常用方法:

1. clear() ——删除此集合的所有键值对
2. containsKey() ——判断该集合是否包含指定的键
3. containsValue() ——判断该集合是否包含指定的值
4. entrySet() ——遍历map集合
5. forEach() ——对map集合所有键值对进行操作
6. get() ——获取指定键对应的值
7. isEmpty() ——判断此集合是否为空
8. keySet() ——获取此地图中所有的键
9. put() ——添加键值对,键重复则覆盖
10. putAll() ——将指定map集合的所有键值对复制到此集合
11. remove() ——删除指定的键,或者删除指定的键值对
12. replace() ——更改指定键的值,或者更改指定键值对的值
13. replaceAll() ——对所有键的值进行操作更改
14. size() ——获取此集合键的数量,包括null键
15. values() ——获取此集合所有的值

二、简单使用


import java.util.*;
import java.util.function.BiConsumer;

//五、HashMap集合常用方法:
public class HashMapMethod {
    static Map<Integer,String> map = new HashMap<>();
    static Map<Integer,String> map1 = new HashMap<>();
    static Map<String,Integer> map2 = new HashMap<>();
    static Map<String,Integer> map3 = new HashMap<>();
    static {
        map.put(1,"张三");map.put(2,"李四");map.put(3,"王五");map.put(4,"赵六");
        map1.put(6,"刘备");map1.put(7,"关羽");map1.put(8,"张飞");map1.put(9,"赵云");
        map2.put("西巴",11);map2.put("西瓜",12);map2.put("苹果",13);map2.put("椰子",14);
        map3.put("学生",16);map3.put("老师",17);map3.put("工人",18);map3.put("医生",19);
    }
    public static void main(String[] args) {
//        1. clear() ——删除此集合的所有键值对
        map3.clear();
//        System.out.println(map3);
//        2. containsKey() ——判断该集合是否包含指定的键
        boolean s2 = map.containsKey(3);
//        System.out.println(s2);
//        3. containsValue() ——判断该集合是否包含指定的值
        boolean s3 = map.containsValue("王五");
//        System.out.println(s3);
//        4. entrySet() ——遍历map集合
        Set<Map.Entry<Integer, String>> m= map.entrySet();
//        System.out.println(m);
//        for(Map.Entry<Integer, String> s : m){
            System.out.println(s);
//            Integer key = s.getKey();
//            String value = s.getValue();
//            System.out.println(key + "=" + value);
//        }
//        5. forEach() ——对map集合所有键值对进行操作
//        map.forEach((a,b) -> System.out.println(a + "-" + b));
        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer key, String value) {
//                System.out.println(key + "-" + value);
            }
        });
//        6. get() ——获取指定键对应的值
        String s8 = map.get(3);
//        System.out.println(s8);
//        7. isEmpty() ——判断此集合是否为空
        boolean s7 = map3.isEmpty();
//        System.out.println(s7);
//        8. keySet() ——获取此地图中所有的键
        Set<Integer> key = map.keySet();
//        System.out.println(key);
//        9. put() ——添加键值对,键重复则覆盖
        map1.put(5,"曹操");
//        System.out.println(map1);
//        10. putAll() ——将指定map集合的所有键值对复制到此集合
        map1.putAll(map);
//        System.out.println(map1);
//        11. remove() ——删除指定的键,或者删除指定的键值对
        map1.remove(3);
        map1.remove(6,"刘备");
//        System.out.println(map1);
//        12. replace() ——更改指定键的值,或者更改指定键值对的值
        map1.replace(1,"荀彧");
//        System.out.println(map1);
//        13. replaceAll() ——对所有键的值进行操作更改
        map.replaceAll((a,b) -> b + "2");
//        System.out.println(map);
        map2.replaceAll((a,b) -> b + 10);
//        System.out.println(map2);
//        14. size() ——获取此集合键的数量,包括null键
        map.put(null,null);
        int s14 = map.size();
//        System.out.println(s14);
//        15. values() ——获取此集合所有的值
        Collection<String> values = map.values();
//        System.out.println(values);
//        Iterator<String> is = values.iterator();
//        while (is.hasNext()){
//            String s = is.next();
//            System.out.println(s);
//        }
    }
}

猜你喜欢

转载自blog.csdn.net/yl23921/article/details/127046493