相思相見知何日?此時此夜難為情。
返回List集合: toList()
用於將元素累積到List
集合中。它將創建一個新List
集合(不會更改當前集合)。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
integers.stream().map(x -> x*x).collect(Collectors.toList());
// output: [1,4,9,16,25,36,36]
返回Set集合: toSet()
用於將元素累積到Set
集合中。它會刪除重復元素。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
integers.stream().map(x -> x*x).collect(Collectors.toSet());
// output: [1,4,9,16,25,36]
返回指定的集合: toCollection()
可以將元素雷擊到指定的集合中。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
integers
.stream()
.filter(x -> x >2)
.collect(Collectors.toCollection(LinkedList::new));
// output: [3,4,5,6,6]
計算元素數量: Counting()
用於返回計算集合中存在的元素個數。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
Long collect = integers
.stream()
.filter(x -> x <4)
.collect(Collectors.counting());
// output: 3
求最小值: minBy()
用於返回列表中存在的最小值。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
List<String> strings = Arrays.asList("alpha","beta","gamma");
integers
.stream()
.collect(Collectors.minBy(Comparator.naturalOrder()))
.get();
// output: 1
strings
.stream()
.collect(Collectors.minBy(Comparator.naturalOrder()))
.get();
// output: alpha
按照整數排序返回1,按照字符串排序返回alpha
可以使用reverseOrder()方法反轉順序。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
List<String> strings = Arrays.asList("alpha","beta","gamma");
integers
.stream()
.collect(Collectors.minBy(Comparator.reverseOrder()))
.get();
// output: 6
strings
.stream()
.collect(Collectors.minBy(Comparator.reverseOrder()))
.get();
// output: gamma
同時可以自定義的對象定制比較器。
求最大值: maxBy()
和最小值方法類似,使用maxBy()
方法來獲得最大值。
List<String> strings = Arrays.asList("alpha","beta","gamma");
strings
.stream()
.collect(Collectors.maxBy(Comparator.naturalOrder()))
.get();
// output: gamma
分區列表:partitioningBy()
用於將一個集合划分為2個集合並將其添加到映射中,1個滿足給定條件,另一個不滿足,例如從集合中分離奇數。因此它將在map
中生成2條數據,1個以true
為key
,奇數為值,第2個以false
為key
,以偶數為值。
List<String> strings = Arrays.asList("a","alpha","beta","gamma");
Map<Boolean, List<String>> collect1 = strings
.stream()
.collect(Collectors.partitioningBy(x -> x.length() > 2));
// output: {false=[a], true=[alpha, beta, gamma]}
這里我們將長度大於2的字符串與其余字符串分開。
返回不可修改的List集合:toUnmodifiableList()
用於創建只讀List
集合。任何試圖對此不可修改List
集合進行更改的嘗試都將導致UnsupportedOperationException
。
List<String> strings = Arrays.asList("alpha","beta","gamma");
List<String> collect2 = strings
.stream()
.collect(Collectors.toUnmodifiableList());
// output: ["alpha","beta","gamma"]
返回不可修改的Set集合:toUnmodifiableSet()
用於創建只讀Set
集合。任何試圖對此不可修改Set
集合進行更改的嘗試都將導致UnsupportedOperationException
。它會刪除重復元素。
List<String> strings = Arrays.asList("alpha","beta","gamma","alpha");
Set<String> readOnlySet = strings
.stream()
.sorted()
.collect(Collectors.toUnmodifiableSet());
// output: ["alpha","beta","gamma"]
連接元素:Joining()
用指定的字符串鏈接集合內的元素。
List<String> strings = Arrays.asList("alpha","beta","gamma");
String collect3 = strings
.stream()
.distinct()
.collect(Collectors.joining(","));
// output: alpha,beta,gamma
String collect4 = strings
.stream()
.map(s -> s.toString())
.collect(Collectors.joining(",","[","]"));
// output: [alpha,beta,gamma]
Long類型集合的平均值:averagingLong()
查找Long
類型集合的平均值。
注意:返回的是Double
類型而不是 Long
類型
List<Long> longValues = Arrays.asList(100l,200l,300l);
Double d1 = longValues
.stream()
.collect(Collectors.averagingLong(x -> x * 2));
// output: 400.0
Integer類型集合的平均值:averagingInt()
查找Integer
類型集合的平均值。
注意:返回的是Double
類型而不是 int
類型
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
Double d2 = integers
.stream()
.collect(Collectors.averagingInt(x -> x*2));
// output: 7.714285714285714
Double類型集合的平均值:averagingDouble()
查找Double
類型集合的平均值。
List<Double> doubles = Arrays.asList(1.1,2.0,3.0,4.0,5.0,5.0);
Double d3 = doubles
.stream()
.collect(Collectors.averagingDouble(x -> x));
// output: 3.35
創建Map:toMap()
根據集合的值創建Map
。
List<String> strings = Arrays.asList("alpha","beta","gamma");
Map<String,Integer> map = strings
.stream()
.collect(Collectors
.toMap(Function.identity(),String::length));
// output: {alpha=5, beta=4, gamma=5}
創建了一個Map
,其中集合值作為key
,在集合中的出現次數作為值。
在創建
map時處理列表的重復項
集合中可以包含重復的值,因此,如果想從列表中創建一個Map
,並希望使用集合值作為map的key
,那么需要解析重復的key
。由於map只包含唯一的key
,可以使用比較器來實現這一點。
List<String> strings = Arrays.asList("alpha","beta","gamma","beta");
Map<String,Integer> map = strings
.stream()
.collect(Collectors
.toMap(Function.identity(),String::length,(i1,i2) -> i1));
// output: {alpha=5, gamma=5, beta=4}
Function.identity()
指向集合中的值,i1
和i2
是重復鍵的值。可以只保留一個值,這里選擇i1
,也可以用這兩個值來計算任何東西,比如把它們相加,比較和選擇較大的那個,等等。
整數求和:summingInt ()
查找集合中所有整數的和。它並不總是初始集合的和,就像我們在下面的例子中使用的我們使用的是字符串列表,首先我們把每個字符串轉換成一個等於它的長度的整數,然后把所有的長度相加。
List<String> strings = Arrays.asList("alpha","beta","gamma");
Integer collect4 = strings
.stream()
.collect(Collectors.summingInt(String::length));
// output: 18
或直接集合值和
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
Integer sum = integers
.stream()
.collect(Collectors.summingInt(x -> x));
// output: 27
double求和:summingDouble ()
類似於整數求和,只是它用於雙精度值
List<Double> doubleValues = Arrays.asList(1.1,2.0,3.0,4.0,5.0,5.0);
Double sum = doubleValues
.stream()
.collect(Collectors.summingDouble(x ->x));
// output: 20.1
Long求和:summingLong ()
與前兩個相同,用於添加long
值或int
值。可以對int
值使用summinglong()
,但不能對long
值使用summingInt()
。
List<Long> longValues = Arrays.asList(100l,200l,300l);
Long sum = longValues
.stream()
.collect(Collectors.summingLong(x ->x));
// output: 600
Long求和:summingLong ()
與前兩個相同,用於添加long
值或int
值。可以對int
值使用summinglong()
,但不能對long
值使用summingInt()
。
List<Long> longValues = Arrays.asList(100l,200l,300l);
Long sum = longValues
.stream()
.collect(Collectors.summingLong(x ->x));
// output: 600
匯總整數:summarizingInt ()
它給出集合中出現的值的所有主要算術運算值,如所有值的平均值、最小值、最大值、所有值的計數和總和。
List<Integer> integers = Arrays.asList(1,2,3,4,5,6,6);
IntSummaryStatistics stats = integers
.stream()
.collect(Collectors.summarizingInt(x -> x ));
//output: IntSummaryStatistics{count=7, sum=27, min=1, average=3.857143, max=6}
可以使用get
方法提取不同的值,如:
stats.getAverage(); // 3.857143
stats.getMax(); // 6
stats.getMin(); // 1
stats.getCount(); // 7
stats.getSum(); // 27
分組函數:GroupingBy ()
GroupingBy()
是一種高級方法,用於從任何其他集合創建Map
。
List<String> strings = Arrays.asList("alpha","beta","gamma");
Map<Integer, List<String>> collect = strings
.stream()
.collect(Collectors.groupingBy(String::length));
// output: {4=[beta, beta], 5=[alpha, gamma]}
它將字符串長度作為key
,並將該長度的字符串列表作為value
。
List<String> strings = Arrays.asList("alpha","beta","gamma");
Map<Integer, LinkedList<String>> collect1 = strings
.stream()
.collect(Collectors.groupingBy(String::length,
Collectors.toCollection(LinkedList::new)));
// output: {4=[beta, beta], 5=[alpha, gamma]}
這里指定了Map
中需要的列表類型(Libkedlist
)。
🙂🙂🙂微信公眾號java干貨