package com;
import com.person.Person;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class test {
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-16 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) throws ParseException {
List list = Arrays.asList(7, 6, 9, 4, 11, 6);
// 自然排序
Optional max = getList().stream().max(Comparator.comparing(demo::getAzcp0006));
System.out.println("自然排序的最大值:" + max.get().getAzcp0006());
Optional max2 = getList2().stream().max(Comparator.comparing(Person::getSalary));
System.out.println("员工薪资最大值:" + max2.get().getSalary());
Map limap=getList().stream().filter(d -> d.getAzcp0005().equals("fmale")).collect(Collectors.toMap(demo::getAzcp0004,demo::getAzcp0005));
Set maps=limap.keySet();
for (String s:maps
) {
System.out.println("filter结果 key= "+s+" value= "+limap.get(s));
}
List newList=getList2().stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
for (Person p: newList
) {
System.out.println("Person的姓名="+p.getName()+" 年龄="+p.getAge());
}
List newlist2=getList().stream().sorted(Comparator.comparing(demo::getAzcp0006)).collect(Collectors.toList());
for (demo d: newlist2
) {
System.out.println("demo的姓名="+d.getAzcp0004()+" time="+d.getAzcp0006());
}
// 先按工资再按年龄升序排序
List newList3 = getList2().stream()
.sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge))
.collect(Collectors.toList());
for (Person p:newList3
) {
System.out.println("排序后的结果集:age="+p.getAge()+" salary= "+p.getSalary());
}
}
}
运行结果:
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeFri Dec 16 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
自然排序的最大值:Fri Dec 16 13:25:07 CST 2022
姓名:Tom ,salary8900
姓名:Jack ,salary7000
姓名:Lily ,salary7800
姓名:Anni ,salary8200
姓名:Owen ,salary9500
姓名:Alisa ,salary7900
员工薪资最大值:9500
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeFri Dec 16 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
filter结果 key= 小孙 value= fmale
filter结果 key= 小张 value= fmale
姓名:Tom ,salary8900
姓名:Jack ,salary7000
姓名:Lily ,salary7800
姓名:Anni ,salary8200
姓名:Owen ,salary9500
姓名:Alisa ,salary7900
Person的姓名=Lily 年龄=21
Person的姓名=Tom 年龄=23
Person的姓名=Anni 年龄=24
Person的姓名=Jack 年龄=25
Person的姓名=Owen 年龄=25
Person的姓名=Alisa 年龄=26
姓名:小王 ,timeTue Dec 13 13:25:07 CST 2022
姓名:小孙 ,timeFri Dec 16 13:25:07 CST 2022
姓名:小张 ,timeThu Dec 15 13:25:07 CST 2022
demo的姓名=小王 time=Tue Dec 13 13:25:07 CST 2022
demo的姓名=小张 time=Thu Dec 15 13:25:07 CST 2022
demo的姓名=小孙 time=Fri Dec 16 13:25:07 CST 2022
姓名:Tom ,salary8900
姓名:Jack ,salary7000
姓名:Lily ,salary7800
姓名:Anni ,salary8200
姓名:Owen ,salary9500
姓名:Alisa ,salary7900
排序后的结果集:age=25 salary= 7000
排序后的结果集:age=21 salary= 7800
排序后的结果集:age=26 salary= 7900
排序后的结果集:age=24 salary= 8200
排序后的结果集:age=23 salary= 8900
排序后的结果集:age=25 salary= 9500