Map的四种遍历方式、entrySet()、Stream流中的Map举例

目录

一、参考资料

二、Map的常见方法

1、 map.entryset() 方法:

2、map.entrySet().stream()方法:

3、Map.Entry里面包含getKey()和getValue()方法:

4、map.keySet()方法:

三、Map的四种遍历方式

1、第一种:

2、第二种:

3、第三种:

4、第四种,推荐使用:

四、Stream流中Map的举例


一、参考资料

这位大佬写的特别棒,我在他的基础上补充了一些:

http://t.csdnimg.cn/WiqkZ

二、Map的常见方法

    了解Map的常见方法,我们才能更好的掌握Map的四种遍历方式:

1、 map.entryset() 方法:

    map.entryset() 返回值是一个set集合,集合里面的每一个元素都是实现了Map.Entry接口的对象,实际上每个Entry对象就是把map集合里面的键和值封装起来的一个 键值对 对象:

          Map的四种遍历方式、entrySet()、Stream流中的Map举例_第1张图片

  Map map = new HashMap<>();
  map.put("蜡笔小新",5);
  map.put("小黑子",22);
  map.put("日向翔阳",16);
  Set> entries = map.entrySet();
2、map.entrySet().stream()方法:

     map集合它本身是不能直接转换成Stream流,我们可以选择把map集合,转换成一个单列集合,然后再调用 集合.stream() 方法

  Stream> stream = map.entrySet().stream();
        Map map = new HashMap<>();
        map.put("蜡笔小新",5);
        map.put("海绵宝宝",23);
        map.put("章鱼哥",66);
        Set> entries = map.entrySet();
        Stream> stream1 = entries.stream();
3、Map.Entry里面包含getKey()和getValue()方法:
Set> entryseSet=map.entrySet();
 
for (Map.Entry entry:entryseSet) {
 
    System.out.println(entry.getKey()+","+entry.getValue());
 
}

     Map.Entry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值 

4、map.keySet()方法:

        获取map集合中所有的key:

Set set = map.keySet();
 
for (String key :set) {
 
    System.out.println(key +","+map.get(key));
 
}

三、Map的四种遍历方式

    Map map = new HashMap();
    map.put("1", "value1");
    map.put("2", "value2");
    map.put("3", "value3");
1、第一种:

从Map中取得关键字之后,我们必须每次重复返回到Map中取得相对的值,这是很繁琐、费时:

   //第一种:普遍使用,二次取值
    System.out.println("通过Map.keySet遍历key和value:");
    for (String key : map.keySet()) {
        System.out.println("key= "+ key + " and value= " + map.get(key));
    }
2、第二种:
 System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
 for (String v : map.values()) {
    System.out.println("value= " + v);
 }
 
3、第三种:
 System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator> it = map.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry entry = it.next();
    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
   }
 
4、第四种,推荐使用:
 System.out.println("通过Map.entrySet遍历key和value");

 for (Map.Entry entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());

 }
 

四、Stream流中Map的举例

   public void Test08(){
       Map map = new HashMap<>();
       map.put("蜡笔小新",5);
       map.put("海绵宝宝",23);
       map.put("章鱼哥",66);
       Set> entries = map.entrySet();
       Stream> stream = entries.stream();
       stream.filter(new Predicate>(){
           public boolean test(Map.Entry entry){
               return entry.getValue()>16;
           }
       }).forEach(new Consumer>(){
           public void accept(Map.Entry entry){
               System.out.println(entry.getKey()+"----"+entry.getValue());
           }
       });
    }

         Lambda表达式简化一下:

Set> entries = map.entrySet();
 Stream> stream = entries.stream();
 stream.filter(entry -> entry.getValue()>16)
       .forEach(entry -> System.out.println(entry.getKey()+"----"+entry.getValue()));
map = map.entrySet().stream()
        .filter(u -> u.getValue() != 0)
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


你可能感兴趣的:(java,spring)