package org.example.jdk.res;
import org.example.lambda.domain.Person;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test01 {
/*
* Stream结果收集
* 收集到集合中
* */
@Test
public void test01(){
//Stream<String> stream = Stream.of("aa", "bb", "cc");
List<String> collect = Stream.of("aa", "bb", "cc")
.collect(Collectors.toList());
System.out.println(collect);
//收集到set集合中
Set<String> set = Stream.of("aa", "bb", "cc")
.collect(Collectors.toSet());
System.out.println(set);
//如果需要获取的类型为具体实现,比如:ArrayList,HashSet
ArrayList<String> arrayList = Stream.of("aa", "bb", "cc")
.collect(Collectors.toCollection(() -> new ArrayList<>()));
System.out.println(arrayList);
}
/*
* Stream结果收集到数组中
* */
@Test
public void test02(){
Object[] objects = Stream.of("aa", "bb", "cc","aa")
.toArray();//返回的数组中的元素是Object类型
System.out.println(objects);
//如果需要指定返回类型
String[] strings = Stream.of("aa", "bb", "cc", "aa")
.toArray(String[] :: new);
System.out.println(strings);
}
/*
* Stream流聚合
* */
@Test
public void test03(){
//获取年龄最大值
Optional<Person> collect = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.maxBy((p1, p2) -> p1.getAge() - p2.getAge()));
System.out.println(collect);
//获取年龄最小值
Optional<Person> c = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.minBy((p1, p2) -> p1.getAge() - p2.getAge()));
System.out.println(c);
//获取年龄和
Integer sum = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.summingInt(s -> s.getAge()));
System.out.println(sum);
}
/*
* 分组计算
* */
@Test
public void test04(){
//根据姓名分组
Map<String, List<Person>> map = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.groupingBy(s -> s.getName()));
map.forEach((k,v)-> System.out.println("k=" + k + "v=" + v));
//根据年龄分组
Map<Integer, List<Person>> map1 = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.groupingBy(s -> s.getAge()));
map1.forEach((a,b)-> System.out.println("a=" + a + "b=" + b));
}
/*
* 分区操作
* */
@Test
public void test05(){
Map<Boolean, List<Person>> tq = Stream.of(
new Person("qql", 50, 175),
new Person("ltb", 60, 156),
new Person("ldw", 20, 156)
).collect(Collectors.partitioningBy(p -> p.getAge() > 20));
tq.forEach((k,v)-> System.out.println(k+"\t"+v));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jdk8.zip (53个子文件)
jdk8
pom.xml 1KB
src
test
java
org
example
jdk
res
Test01.java 4KB
Test03.java 1KB
Test02.java 1KB
main
resources
java
org
example
lambda
DemoLambda05.java 860B
DemoLambda03.java 1KB
DemoLambda02.java 574B
service
OrderService.java 107B
UserService.java 236B
StudentService.java 118B
DemoLambda01.java 718B
domain
Person.java 293B
streamAPI
StreamTest02.java 848B
StreamTest01.java 875B
StreamTest06.java 993B
StreamTest.java 418B
StreamTest05.java 3KB
StreamTest04.java 578B
StreamTest03.java 338B
Optional
OptionalTest03.java 404B
OptionalTest04.java 1KB
OptionalTest02.java 1KB
OptionalTest01.java 830B
.idea
jarRepositories.xml 864B
uiDesigner.xml 9KB
workspace.xml 6KB
misc.xml 544B
compiler.xml 537B
.gitignore 190B
encodings.xml 267B
target
classes
org
example
lambda
DemoLambda01.class 1KB
DemoLambda02$1.class 743B
DemoLambda05.class 3KB
DemoLambda03.class 2KB
DemoLambda02.class 2KB
service
OrderService.class 189B
StudentService.class 194B
UserService.class 225B
DemoLambda01$1.class 1KB
domain
Person.class 3KB
streamAPI
StreamTest01.class 2KB
StreamTest02.class 3KB
StreamTest06.class 3KB
StreamTest04.class 2KB
StreamTest05.class 2KB
StreamTest03.class 913B
StreamTest.class 1KB
Optional
OptionalTest03.class 2KB
OptionalTest01.class 858B
OptionalTest02.class 2KB
OptionalTest04.class 3KB
test-classes
org
example
jdk
res
Test02.class 3KB
Test01.class 7KB
generated-test-sources
test-annotations
generated-sources
annotations
共 53 条
- 1
资源评论
qql桑
- 粉丝: 164
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功