我有以下内容:
Map<String, String> carMap;
List<Car> cars = ......
carMap = cars.stream
.collect(Collectors.toMap(key -> key.getValue(), key -> key.getColor()));
我要遍历汽车,我想在屏幕上显示地图中包含的内容。收集地图后,我可以做类似的事情,但是我不想再浏览列表:
cars.stream()
.forEach(System.out::println)
有没有办法在此行中执行sysout?
carMap = cars.stream
.collect(Collectors.toMap(key -> key.getValue(), key -> key.getColor()));
我在流之后尝试了forEach,但显然没有用
如果仅出于调试目的,并且您想要收集和打印,则可以peek
在这里简单地使用-
Map<String, String> carMap = cars.stream()
.peek(c -> System.out.println("Value: " + c.getValue() + " and Color: " + c.getColor()))
.collect(Collectors.toMap(key -> key.getValue(), key -> key.getColor()));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句