package com;
import com.person.Person;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class test {
public test() throws ParseException {
}
public static List getList() throws ParseException {
List personList = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
Date now = new Date();
personList.add(new demo("小王", "male", sdf.parse("2022-12-13 13:25:07")));
personList.add(new demo("小孙", "fmale", sdf.parse("2022-12-14 13:25:07")));
personList.add(new demo("小张", "fmale", sdf.parse("2022-12-15 13:25:07")));
for (demo e:personList
) {
System.out.println("姓名:"+e.getAzcp0004()+" ,time"+e.getAzcp0006());
}
return personList;
}
public static List getList2() throws ParseException {
List personList = new ArrayList();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
personList.add(new Person("Owen", 9500, 25, "male", "New York"));
personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
for (Person e:personList
) {
System.out.println("姓名:"+e.getName()+" ,salary"+e.getSalary());
}
return personList;
}
/* public static void main(String[] args) {
List personList = new ArrayList();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
personList.add(new Person("Owen", 9500, 25, "male", "New York"));
personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
List fiterList = personList.stream().filter(x -> x.getSalary() > 8000).map(Person::getName)
.collect(Collectors.toList());
System.out.print("薪资高于8000美元的员工:" + fiterList);
}*/
/* public static void main(String[] args) {
// import已省略,请自行添加,后面代码亦是
List list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
// 遍历输出符合条件的元素
list.stream().filter(x -> x > 6).forEach(System.out::println);
// 匹配第一个
Optional findFirst = list.stream().filter(x -> x > 6).findFirst();
// 匹配任意(适用于并行流)
Optional findAny = list.parallelStream().filter(x -> x > 6).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = list.stream().anyMatch(x -> x > 6);
System.out.println("匹配第一个值:" + findFirst.get());
System.out.println("匹配任意一个值:" + findAny.get());
System.out.println("是否存在大于6的值:" + anyMatch);
}
*/
public static void main(String[] args) throws ParseException {
List list = Arrays.asList("jack", "bob", "alice", "mark");
List duplicateList = Arrays.asList("jack", "jack", "alice", "mark");
List demos=getList();
/**
* Collectors.toList() 将stream转换为list。这里转换的list是ArrayList
*/
List listResult = demos.stream().map(demo::getAzcp0004).collect(Collectors.toList());
for ( String s:listResult
) {
System.out.println("结果tolist(): "+s);
}
/**
* Collectors.toSet() toSet将Stream转换成为set。这里转换的是HashSet。
*/
Set listset = duplicateList.stream().map(String::toUpperCase).collect(Collectors.toSet());
for ( String s:listset
) {
System.out.println("结果toset(): "+s);
}
/**
* Collectors.toMap() toMap接收两个参数,第一个参数是keyMapper,第二个参数是valueMapper:
*/
Map listmap = demos.stream().collect(Collectors.toMap(Function.identity(),demo::getAzcp0004));
System.out.println("map结果tomap "+listmap);
Set keys=listmap.keySet();
for (demo d:keys
) {
System.out.println("遍历map的结果 "+d.getAzcp0004());
}
Set> entityset=listmap.entrySet();
for (Map.Entry d:entityset
) {
System.out.println("遍历entryset的结果 "+d.getKey()+" value= "+d.getValue());
}
/**
* Collectors.counting() counting主要用来统计stream中元素的个数
*/
Long countResult = list.stream().collect(Collectors.counting());
System.out.println("counting的结果 "+countResult);
/**
*Collectors.summarizingDouble/Long/Int()
* SummarizingDouble/Long/Int为stream中的元素生成了统计信息,返回的结果是一个统计类
*/
IntSummaryStatistics intResult = getList2().stream()
.collect(Collectors.summarizingInt(Person::getSalary));
System.out.println("intResult的结果为 "+ intResult);
/**
*Collectors.groupingBy()
* GroupingBy根据某些属性进行分组,并返回一个Map
*/
Map> groupByResult = getList().stream()
.collect(Collectors.groupingBy(demo::getAzcp0005, Collectors.toSet()));
Set>> maps= groupByResult.entrySet();
for ( Map.Entry> e:maps
) {
System.out.println("groupby结果 ,key= "+e.getKey()+" value的值= "+e.getValue());
}
/**
* Collectors.partitioningBy() 按照某条件划分两类
*/
Map> partitionResult = getList().stream()
.collect(Collectors.partitioningBy(s-> s.getAzcp0005() .equals("male")));
Set partitionmaps=partitionResult.keySet();
for (Boolean b:partitionmaps
) {
System.out.println("partitioningBy 结果为"+ partitionResult.get(b));
}
}
}
运行结果
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeWed Dec 14 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
结果tolist(): 小王
结果tolist(): 小孙
结果tolist(): 小张
结果toset(): ALICE
结果toset(): JACK
结果toset(): MARK
map结果tomap {com.demo@b1bc7ed=小王, com.demo@30dae81=小张, com.demo@7cd84586=小孙}
遍历map的结果 小王
遍历map的结果 小张
遍历map的结果 小孙
遍历entryset的结果 com.demo@b1bc7ed value= 小王
遍历entryset的结果 com.demo@30dae81 value= 小张
遍历entryset的结果 com.demo@7cd84586 value= 小孙
counting的结果 4
姓名:Tom ,salary8900
姓名:Jack ,salary7000
姓名:Lily ,salary7800
姓名:Anni ,salary8200
姓名:Owen ,salary9500
姓名:Alisa ,salary7900
intResult的结果为 IntSummaryStatistics{count=6, sum=49300, min=7000, average=8216.666667, max=9500}
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeWed Dec 14 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
groupby结果 ,key= fmale value的值= [com.demo@76fb509a, com.demo@300ffa5d]
groupby结果 ,key= male value的值= [com.demo@6576fe71]
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeWed Dec 14 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
partitioningBy 结果为[com.demo@37bba400, com.demo@179d3b25]
partitioningBy 结果为[com.demo@254989ff]