StreamAPI练习

Trader 交易员 属性有姓名、工作地点
Transaction 交易 属性有交易员、交易年份、交易金额

import com.leyou.common.model.Trader;
import com.leyou.common.model.Transaction;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.summingInt;

/**
 * @author: CandySir
 * @description:
 * @date : 2019/10/12
 */
public class TestTransaction {
    List<Transaction> transactions = null;
    @Before
    public void before(){
        Trader raoul = new Trader("Raoul","Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");

        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)

        );
    }
    //找出2011年发生的所有交易,并按交易额排序(低->高)
    @Test
    public void test1(){

           List<Transaction> transactionList =
                   transactions.stream().filter(t -> t.getYear() ==2011)
                   .sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
                   .collect(Collectors.toList());
        transactionList.stream().forEach(System.out::println);
    }
    //交易员都在哪些不同的城市工作过
    @Test
    public void test2(){
        List<String> list = transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getCity)
                .distinct()
                .collect(Collectors.toList());
        list.stream().forEach(System.out::println);
        System.out.println("-------------------------------------");
        transactions.stream()
                .map(t -> t.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);

    }
    //查找所有来自剑桥的交易员,并按姓名排序
    @Test
    public void test3(){
        transactions.stream()
                .filter(t -> t.getTrader().getCity() == "Cambridge")
                .map(Transaction::getTrader)
                .sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
                .distinct()
                .forEach(System.out::println);
    }
    //返回所有交易员的姓名字符串,按字母顺序排序
    @Test
    public void test4(){
        String str = transactions.stream()
                .map(t -> t.getTrader().getName())
                .sorted((t1,t2) -> t1.compareTo(t2))
                .distinct()
                .reduce("---",String::concat);
        System.out.println(str);
    }
    //有没有交易员在米兰工作过
    @Test
    public void test5(){
        transactions.stream()
                .filter(t ->t.getTrader().getCity().equalsIgnoreCase("Milan"))
                .map(Transaction::getTrader)
                .distinct()
                .collect(Collectors.toList())
                .forEach(System.out::println);

        Boolean b = transactions.stream()
                .anyMatch(t ->t.getTrader().getCity().equalsIgnoreCase("Milan"));
        System.out.println(b);
    }
    //打印生活在剑桥的交易员的所有交易额
    @Test
    public void test6(){
        Optional<Integer> a = transactions.stream()
                .filter(t ->t.getTrader().getCity().equalsIgnoreCase("Cambridge"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);
        System.out.println(a.get());
        System.out.println("-----------------------");
        Integer b = transactions.stream()
                .filter(t ->t.getTrader().getCity().equalsIgnoreCase("Cambridge"))
                .map(Transaction::getValue)
                .collect(summingInt(Integer::valueOf));
        System.out.println(b);

    }
    //所有交易中,最高的交易额是多少
    @Test
    public void test7(){
        Optional<Integer> a = transactions.stream()
                .map(Transaction::getValue)
                .max(Integer::compareTo);
        System.out.println(a.get());
        System.out.println("---------------------");
        Optional<Integer> b = transactions.stream()
                .map(Transaction::getValue)
                .collect(maxBy(Integer::compareTo));
        System.out.println(b.get());
    }
    //找到交易额最小的交易
    @Test
    public void test8(){
        Optional<Transaction> op = transactions.stream()
                .min((t1, t2) -> Integer.compare(t1.getValue(),t2.getValue()));
        System.out.println(op.get());
    }
    
}

		//把姓张的同学年龄加2
		Student a = new Student("张三", 18);
        Student b = new Student("张四", 28);
        Student c = new Student("张五", 38);
        Student d = new Student("田七", 48);

        List<Student> students = Arrays.asList(a, b, c, d);

        List<Student> collect = students.stream()
                .filter(student -> student.getName().startsWith("张"))
                .map(s -> {
                    s.setAge(s.getAge() + 2);
                    return s;
                })
                .collect(Collectors.toList());

        System.out.println(collect);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值