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));