一些簡單用法記錄一下。
文中使用幾個第三方jar包如下:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
測試所需實體類:
package com.gerrard.stream;
import lombok.Data;
/**
* 測試bean.
*/
@Data
public class StreamBean {
private String name;
private String sex;
private int age;
}
測試代碼:
package com.gerrard.stream;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Gerrard on 2020/06/09.
*
* Stream簡單用法測試.
*/
public class StreamTest {
public static void main(String[] args) {
StreamTest streamTest = new StreamTest();
// 初始化測試資料.
List<StreamBean> streamBeanList = streamTest.initList();
System.out.println("初始化List如下:");
// 控制台循環列印初始化資料.
streamBeanList.forEach(item -> System.out.println(item.getName() + "|" + item.getSex() + "|" + item.getAge()));
System.out.println("\nFilter測試(查詢list性别為女的所有資料):");
streamTest.testFilter(streamBeanList, "女");
System.out.println("\nSorted測試:");
streamTest.testSorted(streamBeanList);
System.out.println("\nMap測試:");
streamTest.testMap(streamBeanList);
System.out.println("\nCollectors測試:");
streamTest.testCollectors(streamBeanList);
System.out.println("\nStatistics測試:");
streamTest.testStatistics(streamBeanList);
}
/**
* Statistics測試.
*
* @param streamBeanList
*/
public void testStatistics(List<StreamBean> streamBeanList) {
IntSummaryStatistics intSummaryStatistics = streamBeanList.stream().mapToInt(x -> x.getAge()).summaryStatistics();
System.out.println("年齡最大的是:" + intSummaryStatistics.getMax());
System.out.println("年齡最小的是:" + intSummaryStatistics.getMin());
System.out.println("年齡總和是:" + intSummaryStatistics.getSum());
System.out.println("平均年齡是:" + intSummaryStatistics.getAverage());
System.out.println("人數總和是:" + intSummaryStatistics.getCount());
}
/**
* Collectors過濾抽取合并字元串測試.
*
* @param streamBeanList
*/
public void testCollectors(List<StreamBean> streamBeanList) {
if (CollectionUtils.isNotEmpty(streamBeanList)) {
// 所有男性的姓名合并字元串用逗号分隔.
String collect = streamBeanList.stream().filter(item -> item.getSex().equals("男")).map(item -> item.getName()).collect(Collectors.joining(", "));
System.out.println(collect);
}
}
public void testMap(List<StreamBean> streamBeanList) {
// 通過map抽取bean清單的所有姓名
if (CollectionUtils.isNotEmpty(streamBeanList)) {
System.out.println("抽取清單實體所有姓名:");
List<String> collect = streamBeanList.stream().map(item -> item.getName()).collect(Collectors.toList());
collect.stream().forEach(item -> System.out.println(item));
System.out.println("将清單所有姓名添加字首A_:");
List<StreamBean> collect1 = streamBeanList.stream().map(item -> {
item.setName("A_" + item.getName());
return item;
}).sorted(Comparator.comparing(StreamBean::getSex)).collect(Collectors.toList());
collect1.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex()));
}
}
/**
* 測試排序.
*
* @param streamBeanList
*/
public void testSorted(List<StreamBean> streamBeanList) {
if (CollectionUtils.isNotEmpty(streamBeanList)) {
System.out.println("按性别排序");
List<StreamBean> result = streamBeanList.stream().sorted(Comparator.comparing(StreamBean::getSex)).collect(Collectors.toList());
result.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex() + " | " + item.getAge()));
System.out.println("按性别排序2");
List<StreamBean> result2 = streamBeanList.stream().sorted(Comparator.comparing(StreamBean::getSex).reversed()).collect(Collectors.toList());
result2.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex() + " | " + item.getAge()));
System.out.println("按年齡排序1");
List<StreamBean> result3 = streamBeanList.stream().sorted(Comparator.comparing(StreamBean::getAge)).collect(Collectors.toList());
result3.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex() + " | " + item.getAge()));
System.out.println("按年齡排序2");
List<StreamBean> result4 = streamBeanList.stream().sorted(Comparator.comparing(StreamBean::getAge).reversed()).collect(Collectors.toList());
result4.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex() + " | " + item.getAge()));
}
}
/**
* Filter測試.
*
* @param sex 根據性别篩選.
*/
public void testFilter(List<StreamBean> streamBeanList, String sex) {
if (CollectionUtils.isNotEmpty(streamBeanList)) {
List<StreamBean> result = streamBeanList.stream().filter(item -> item.getSex().equals(sex)).collect(Collectors.toList());
result.stream().forEach(item -> System.out.println(item.getName() + " | " + item.getSex()));
}
}
/**
* 初始化資料.
*
* @return List<StreamBean>.
*/
public List<StreamBean> initList() {
List<StreamBean> streamBeanList = new ArrayList<StreamBean>();
StreamBean bean1 = new StreamBean();
bean1.setName("劉某");
bean1.setSex("男");
bean1.setAge(66);
StreamBean bean2 = new StreamBean();
bean2.setName("張某");
bean2.setSex("女");
bean2.setAge(55);
StreamBean bean3 = new StreamBean();
bean3.setName("王某");
bean3.setSex("男");
bean3.setAge(58);
StreamBean bean4 = new StreamBean();
bean4.setName("梁某");
bean4.setSex("女");
bean4.setAge(44);
StreamBean bean5 = new StreamBean();
bean5.setName("周某");
bean5.setSex("男");
bean5.setAge(43);
CollectionUtils.addAll(streamBeanList, bean1, bean2, bean3, bean4, bean5);
return streamBeanList;
}
}
輸出結果:
初始化List如下:
劉某|男|66
張某|女|55
王某|男|58
梁某|女|44
周某|男|43
Filter測試(查詢list性别為女的所有資料):
張某 | 女
梁某 | 女
Sorted測試:
按性别排序
張某 | 女 | 55
梁某 | 女 | 44
劉某 | 男 | 66
王某 | 男 | 58
周某 | 男 | 43
按性别排序2
劉某 | 男 | 66
王某 | 男 | 58
周某 | 男 | 43
張某 | 女 | 55
梁某 | 女 | 44
按年齡排序1
周某 | 男 | 43
梁某 | 女 | 44
張某 | 女 | 55
王某 | 男 | 58
劉某 | 男 | 66
按年齡排序2
劉某 | 男 | 66
王某 | 男 | 58
張某 | 女 | 55
梁某 | 女 | 44
周某 | 男 | 43
Map測試:
抽取清單實體所有姓名:
劉某
張某
王某
梁某
周某
将清單所有姓名添加字首A_:
A_張某 | 女
A_梁某 | 女
A_劉某 | 男
A_王某 | 男
A_周某 | 男
Collectors測試:
A_劉某, A_王某, A_周某
Statistics測試:
年齡最大的是:66
年齡最小的是:43
年齡總和是:266
平均年齡是:53.2
人數總和是:5
完事,沒有啥深度,就簡簡單單的記錄一下。