【Java】JDK8 学习 第五章练习题

package com.read.JDK8.Chap05;

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

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

public class PuttingIntoPractice {
    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> Trans2011 = transactions.stream()
                .filter(transaction -> transaction.getYear() == 2011) // 2011.sorted(comparing(Transaction::getValue)) //交易额排序(从低到高)
                .collect(toList());
        System.out.println(Trans2011);

//        (2) 交易员都在哪些不同的城市工作过?
        List<String> tradersCity = transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .collect(toList());

        System.out.println(tradersCity);

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

//        (4) 返回所有交易员的姓名字符串,按字母顺序排序。
        List<String> tNames = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.toList());
        System.out.println(tNames);

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

//        (6) 打印生活在剑桥的交易员的所有交易额。
        List<Integer> transactionsInTransactions = transactions.stream()
//                .map(Transaction::getValue)
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(transaction -> transaction.getValue())
                .collect(toList());
        System.out.println(transactionsInTransactions);

//        (7) 所有交易中,最高的交易额是多少?
        int maxTransactions = transactions.stream()
                .map(Transaction::getValue)
                .reduce(0, Integer::max);
        System.out.println(maxTransactions);

//        (8) 找到交易额最小的交易
        int minTransactions = transactions.stream().mapToInt(Transaction::getValue).min().getAsInt();
        System.out.println(minTransactions);
//        (9) 把米兰工作的员工城市设置为 hangzhou
        transactions.stream()
//                .map(Transaction::getTrader)
//                .filter(trader -> trader.getCity().equals("Milan"))
//                .forEach(trader -> trader.setCity("hangzhou"));
                .filter(transaction -> transaction.getTrader().getCity().equals("Milan"))
                .forEach(transaction -> transaction.getTrader().setCity("HHHH"));

        System.out.println(transactions);


    }
}
package com.read.JDK8.Chap05;

/***
 * 交易者
 */
public class Trader {
    private String name;
    private 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;
    }

    public void setCity(String newCity){
        this.city = newCity;
    }

    public String toString(){
        return "Trader:"+this.name + " in " + this.city;
    }
}
package com.read.JDK8.Chap05;

/***
 * 交易
 */
public class Transaction {
    private Trader trader;
    private int year;
    private 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;
    }

    public String toString() {
        return "{" + this.trader + ", " +
                "year: " + this.year + ", " +
                "value:" + this.value + "}";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值