我有以下通过Java 8将map转换为list的方法,但我想使用方法引用而不是lambda,请告知如何转换下面的程序以使用Java 8的方法引用功能
以下是我的程序
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("\n1. Export Map Key to List...");
List<Integer> result = map.entrySet()
.stream()
.map(x -> x.getKey())
.collect(Collectors.toList());
System.out.println(result);
答案 0 :(得分:3)
答案 1 :(得分:1)
只需询问地图的keySet()
即可。它返回一组键。
Map<Integer, String> map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "watermelon");
map.put(50, "dragonfruit");
System.out.println("\n1. Export Map Key to List...");
Collection<Integer> result = map.keySet();
System.out.println(result);