关键词搜索

源码搜索 ×
×

Map补充:map遍历方法和computeIfAbsent()方法

发布2022-04-09浏览725次

详情内容

一、Map 的遍历

(1)使用 Iterator 输出 Map 实例

public class Test{
    public static void main(String[] args) {
        Map<String,String > map = new HashMap<String, String>();//实例化 map
        map.put("https://files.jxasp.com:9443/image/2","Java");
        map.put("1","Python");
        map.put("3","Html");
        Set<Map.Entry<String ,String >> allSet = null;//声明一个 Set 集合,指定泛型
        allSet = map.entrySet();//将 Map 接口实例变为 Set 接口实例
        Iterator<Map.Entry<String ,String >> iter = null;//声明 Iterator 对象
        iter = allSet.iterator();//实例化 Iterator 对象
        while (iter.hasNext()){
            Map.Entry<String ,String > me = iter.next();//找到 Map.Entry 实例
            System.out.println(me.getKey() + "-->" + me.getValue());//输出 key 和 value
        }
        }
}

    (2)借助entrySet进行输出

    public class Test{
        public static void main(String[] args) {
            Map<String,String > map = new HashMap<String, String>();//实例化 map
            map.put("https://files.jxasp.com:9443/image/2","Java");
            map.put("1","Python");
            map.put("3","Html");
            for (Map.Entry<String ,String> me:map.entrySet()){//输出 Set 集合
                System.out.println(me.getKey() + "-->" + me.getValue());
            }
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (3)借助foreach

    Map<String,String > map = new HashMap<String, String>();//实例化 map
    map.put("https://files.jxasp.com:9443/image/2","Java");
    map.put("1","Python");
    map.put("3","Html");
    map.forEach((key,value)->{
                System.out.println(key + " --> "+value);
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、map.computeIfAbsent()方法

    computeIfAbsent() 方法对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hashMap 中。

    语法:

    hashmap.computeIfAbsent(K key, Function remappingFunction)
    
    • 1

    如果 key 对应的 value 不存在,则使用获取 remappingFunction 重新计算后的值,并保存为该 key 的 value,否则返回 value。

    比如要将二维数组放到map中,其中将首位array[0]相同的放到一个set里。

    public class TestDemo {
        public static void main(String[] args) {
            Map<Integer, HashSet<Integer>> map = new HashMap<>();
            int[][] arr = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {4, 5}};
            for (int[] array : arr) {
                //如果存在就取出,不存在则新创建一个 set
                HashSet<Integer> set = map.getOrDefault(array[0], new HashSet<>());
                set.add(array[1]);
                //元素添加之后再放入
                map.put(array[0], set);
            }
            map.forEach((key, value) -> {
                System.out.println(key + " --> " + value);
            });
        }
    }
    

      此时使用computeIfAbsent

      for (int[] array : arr) {
      	map.computeIfAbsent(array[0], key -> new HashSet<>()).add(array[1]);
      }
      
      • 1
      • 2
      • 3

      相关技术文章

      点击QQ咨询
      开通会员
      返回顶部
      ×
      微信扫码支付
      微信扫码支付
      确定支付下载
      请使用微信描二维码支付
      ×

      提示信息

      ×

      选择支付方式

      • 微信支付
      • 支付宝付款
      确定支付下载