Stream 练习

2018-10-02  本文已影响23人  tingshuo123

这几天在看Java8的新特性Stream,想试试掌握如何,就找了一个题目练练手。

数据源

    // 数据源
    
    Trader raoul = new Trader("Raoul", "剑桥");
    Trader mario = new Trader("Mario", "米兰");
    Trader alan = new Trader("Alan", "剑桥");
    Trader brian = new Trader("Brian", "剑桥");
    
    List<Transaction> transactions = Arrays.asList(
        new Transaction(raoul, 2011, 400),
        new Transaction(brian, 2011, 300),
        new Transaction(raoul, 2012, 1000),
        new Transaction(mario, 2012, 710),
        new Transaction(mario, 2012, 700),
        new Transaction(alan, 2012, 950)
    );

Trader 和 Transaction 类的定义

public class Trader {
    
    private final String name;
    private final String city;
    
    public Trader(String name, String city) {
        this.name = name;
        this.city = city;
    }
    // getter setter ...
}


public class Transaction {
    
    private final Trader trader;
    private final int year;
    private final int value;
    
    public Transaction(Trader trader, int year, int value) {
        super();
        this.trader = trader;
        this.year = year;
        this.value = value;
    }
    // getter setter ...
}

题目

  1. 找出2011年发生的所有交易,并按交易额排序(低到高)
        List<Transaction> list = transactions.stream()
                .filter(item -> item.getYear() == 2011)
                .sorted((a, b) -> ((Integer)a.getValue()).compareTo(b.getValue()))
                .collect(Collectors.toList());
  1. 交易员都在那些不同的城市工作过?
List<String> list = transactions.stream()
                .map(item -> item.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());
  1. 所有来至剑桥的交易员,按姓名排序
List<Trader> list = transactions.stream()
                .filter(item -> "剑桥".equals(item.getTrader().getCity()))
                .map(Transaction::getTrader)
                .distinct()
                .sorted((a, b) -> a.getName().compareTo(b.getName()))
                .collect(Collectors.toList());
  1. 返回所有交易员姓名的字符串
List<String> list = transactions.stream()
                .map(item -> item.getTrader().getName())
                .distinct()
                .sorted(String::compareToIgnoreCase)
                .collect(Collectors.toList());
  1. 有没有交易员是再米兰工作的
        Optional<Transaction> optional = transactions.stream()
                .filter(item -> "米兰".equals(item.getTrader().getCity()))
                .findAny();
        
        if (optional.isPresent()) {
            System.out.println("有");
        } else {
            System.out.println("没有");
        }
  1. 生活在剑桥的交易员所有交易额
int sum = transactions.stream()
                .filter(item -> "剑桥".equals(item.getTrader().getCity()))
                .map(item -> item.getValue())
                .reduce(0, Integer::sum);
  1. 最高的交易额
int max = transactions.stream()
                .filter(item -> "剑桥".equals(item.getTrader().getCity()))
                .map(item -> item.getValue())
                .reduce(Integer.MIN_VALUE, Integer::max);
  1. 最低的交易额
int min = transactions.stream()
                    .filter(item -> "剑桥".equals(item.getTrader().getCity()))
                    .map(item -> item.getValue())
                    .reduce(Integer.MAX_VALUE, Integer::min);
上一篇下一篇

猜你喜欢

热点阅读