Map<String,Object> map = new HashMap<>();
map.put("name","zhongxu");
map.put("age",28);
map.put("sex","man");
map.put("love","JAVA");
map.put("major","Java后端开发工程师");
//使用lambda 的foreach便利hashmap
System.out.println("使用lambda表达式遍历HashMap:");
map.forEach((key,value)->{
System.out.println(key+"-------"+value); });
//使用stream遍历hashmap
System.out.println("使用stream遍历HashMap:");
map.entrySet().stream().forEach(entry->{
System.out.println(entry.getKey()+"----"+entry.getValue()); });
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100725.html原文链接:https://javaforall.cn