Java8 Stream

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<demo> getList() throws ParseException {
        List<demo> personList = new ArrayList<demo>();
        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<Person> getList2() throws ParseException {
        List<Person> personList = new ArrayList<Person>();
        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<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
            // 自然排序
            Optional<demo> max = getList().stream().max(Comparator.comparing(demo::getAzcp0006));
            System.out.println("自然排序的最大值:" + max.get().getAzcp0006());


            Optional<Person> max2 = getList2().stream().max(Comparator.comparing(Person::getSalary));
            System.out.println("员工薪资最大值:" + max2.get().getSalary());

            Map<String ,String > limap=getList().stream().filter(d -> d.getAzcp0005().equals("fmale")).collect(Collectors.toMap(demo::getAzcp0004,demo::getAzcp0005));
            Set<String> maps=limap.keySet();
            for (String s:maps
                 ) {
                System.out.println("filter结果 key= "+s+" value=   "+limap.get(s));
            }

            List<Person> newList=getList2().stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
            for (Person p: newList
                 ) {
                System.out.println("Person的姓名="+p.getName()+"  年龄="+p.getAge());
            }
            List<demo> 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<Person> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Amo@骄纵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值