java8 Lambda+stream 排序 选择 最大 最小 去重

本文详细介绍Java Stream API的应用,包括如何使用Stream API进行数据过滤、排序、收集等操作,通过具体案例展示Stream API的强大功能,如筛选特定年份的交易、统计不同城市的交易员分布、按名称排序交易员等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.交易员pojo

/**
 * @Author: donghai
 * @Description:
 * @Date: created on 2019/12/20  16:01
 */
public class Trader {

    private final String name;
    private final String city;
    public Trader(String n, String c){
        this.name = n;
        this.city = c;
    }
    public String getName(){
        return this.name;
    }
    public String getCity(){
        return this.city;
    }
    @Override
    public String toString(){
        return "Trader:"+this.name + " in " + this.city;
    }

}

2.交易中心

/**
 * @Author: donghai
 * @Description:
 * @Date: created on 2019/12/20  16:02
 */
public class Transaction {

    private final Trader trader;
    private final int year;
    private final int value;
    public Transaction(Trader trader, int year, int value){
        this.trader = trader;
        this.year = year;
        this.value = value;
    }
    public Trader getTrader(){
        return this.trader;
    }
    public int getYear(){
        return this.year;
    }
    public int getValue(){
        return this.value;
    }
    @Override
    public String toString(){
        return "{" + this.trader + ", " +
                "year: "+this.year+", " +
                "value:" + this.value +"}";
    }

}

3.测试主类。所有注释都在这个类中,看代码即可!

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

/**
 * @Author: donghai
 * @Description:
 * @Date: created on 2019/12/20  16:02
 */
public class MainTrader {
    /**
     * 交易员和交易
     *
     * @param args
     */
    public static void main(String[] args) {
        //
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
        //1.找出2011年发生的所有交易,并按交易额排序(从低到高)。
        List<Transaction> collect = transactions.stream()
                .filter(a -> a.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        collect.forEach(a-> System.out.println(a.getYear()+":"+a.getValue()));

        //(2) 交易员都在哪些不同的城市工作过?
        Stream<String> distinct = transactions.stream().map(a -> a.getTrader().getCity()).distinct();
        distinct.forEach(a-> System.out.println(a));

        //(3) 查找所有来自于剑桥的交易员,并按姓名排序。
        List<Trader> cambridge = transactions.stream()
                .filter(a -> a.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        cambridge.forEach(a-> System.out.println(a.getName()));

        //(4) 返回所有交易员的姓名字符串,按字母顺序排序。---其内部会用到StringBuilder
        String reduce = transactions.stream()
                .map(a -> a.getTrader().getName())
                .distinct()
                .sorted()
               .collect(joining());
        System.out.println(reduce);
        // collect.forEach(a-> System.out.println(a.getName()));

        //(5) 有没有交易员是在米兰工作的?
        boolean milan = transactions.stream()
                .anyMatch(a -> a.getTrader().getCity().equals("Milan"));
        System.out.println(milan);

        //(6) 打印生活在剑桥的交易员的所有交易

        transactions.stream()
                .filter(a -> a.getTrader().getCity().equals("Cambridge"))
              .forEach(a-> System.out.println(a.getValue()));
        //(7) 所有交易中,最高的交易额是多少
        System.out.println( transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max));

        //(8) 找到交易额最小的交易
        System.out.println( transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::min));
        //去重
     List<Long> userIdList = new ArrayList<>();
        userIdList.add(222L);
        userIdList.add(222L);
        userIdList.add(33L);
        userIdList.add(43L);
        List<Long> userIdFinalList = userIdList.stream().distinct().collect(Collectors.toList());
        userIdFinalList.forEach(aLong -> System.out.println(aLong));
        
    // removeif  删除 符合条件的 数据
      List<Integer> longList = new ArrayList<>();
        longList.add(1);
        longList.add(5);
        longList.add(9);
        longList.add(19);
        longList.add(30);
        longList.removeIf(a ->
                a > 5 && a < 30
        );
        longList.forEach(a ->
                System.out.println(a));
                        
        );
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值