0
点赞
收藏
分享

微信扫一扫

Java 中map 遍历优化

梦为马 2023-01-06 阅读 52


文章目录

package demo.map;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>
* Map 遍历优化
* </P>
*
* @author dingwen
* @date 2021.08.27 09:18
*/
public class MapDemo {
public static void main(String[] args) {
HashMap<Object, Object> map = new HashMap<>(3);
map.put("k1","v1");
map.put("k2","v2");
map.put("k3","v3");

System.out.println("map = " + map);

// 遍历只需要获取key
map.keySet().stream().parallel().forEach(System.out::println);

// 遍历是需要获取value
map.values().stream().parallel().forEach(System.out::println);

// 获取key value
map.entrySet().stream().parallel().forEach(System.out::println);
// other 需要对值进行修改时使用map
List<Map.Entry<Object, Object>> collect = map.entrySet().stream().parallel().peek(entry -> {
System.out.println("entry.getKey() = " + entry.getKey());
System.out.println("entry.getValue() = " + entry.getValue());
}).collect(Collectors.toList());

}
}


举报

相关推荐

0 条评论