List<String> list = Arrays.asList("Apple", "Banana", "Orange");
Set<String> set = list.stream()
.collect(Collectors.toSet());
这里将一个List
正确的做法是指定类型参数:
List<String> list = Arrays.asList("Apple", "Banana", "Orange");
Set<String> set = list.stream()
.collect(Collectors.toSet());
List<String> list = Arrays.asList("Apple", "Banana", "Orange");
Map<String, Integer> map = list.stream()
.collect(Collectors.toList());
这里将一个List
List<String> list = Arrays.asList("Apple", "Banana", "Orange");
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length));
List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Apple");
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length));
这里将一个List
正确的做法是处理重复键的情况,可以通过提供一个合并函数来解决:
List<String> list = Arrays.asList("Apple", "Banana", "Orange", "Apple");
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length, (existing, replacement) -> existing));