一些简单用法记录一下。
文中使用几个第三方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
完事,没有啥深度,就简简单单的记录一下。