java8新特性_12_Stream练习



public class TestStreamApi04lianxi {

    List<Employee> emps = Arrays.asList(
            new Employee("张三", 18,9999.99, Status.FREE),
            new Employee("李四", 38,5555.99, Status.BUSY),
            new Employee("王五", 50,6666.66, Status.VOCATION),
            new Employee("赵六", 16,3333.33, Status.FREE),
            new Employee("田七", 10,7777.77, Status.BUSY),
            new Employee("田七", 16,8888.88, Status.VOCATION)
    );

    /**
     * 1.给定一个数列表,返回一个由每个数的平方根构成的列表;
     *   给定【1,2,3,4,5】,应返回【1,4,9,16, 25】
     */
    @Test
    public void test1(){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        list.stream().map(e->e*e).collect(Collectors.toList()).forEach(System.out::print);

    }

    /**
     * 2.用 map 和 reduce 方法数一数流中有多少个Employee?
     */
    @Test
    public void test2(){
        Integer reduce = emps.stream().map(e -> 1).reduce(0, (x, y) -> x + y);
        System.out.println(reduce);
        Optional<Integer> opt = emps.stream().map(e -> 1).reduce((x, y) -> Integer.sum(x, y));
        System.out.println(opt.get());
        opt = emps.stream().map(e -> 1).reduce(Integer::sum);
        System.out.println(opt.get());
        long count = emps.stream().count();
        System.out.println(count);

        System.out.println(reduce);
    }

}
package com.Stream;

import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: //TODO streamAPI 练习
 * @Author: LL
 * @Date: 2019/9/25
 */
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)
        );
    }

    //1.找出2011年发生的所有交易,并按交易额排序(从高到底)
    @Test
    public void test1(){
        transactions.stream()
                .filter(e -> e.getYear()==2011)
                .sorted((x, y) -> Integer.compare(x.getValue(), y.getValue()))
//                .sorted(Comparator.comparingInt(Transaction::getValue))
                .forEach(System.out::println);
    }

    //2.交易员都在哪些不同的城市工作过?
    @Test
    public void test2(){
        transactions.stream()
                .map(x -> x.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }

    //3.查找来自剑桥的交易员,并按姓名排序
    @Test
    public void test3(){
        transactions.stream()
                .filter(t->"Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getTrader)
                .sorted(Comparator.comparing(Trader::getName))
                .distinct()
                .forEach(System.out::println);
    }

    //4.返回所有交易员的姓名字符串,并按字母顺序排序
    @Test
    public void test4(){
        transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .forEach(System.out::println);

        System.out.println("-------------------------------------");
        String s = transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.joining());
        System.out.println(s);

        System.out.println("-------------------------------------");
        String s2 = transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("",String::concat);
        System.out.println(s2);


    }

    //5.有没有交易员是在米兰工作的?
    @Test
    public void test5(){
        boolean b = transactions.stream()
                .anyMatch(x -> x.getTrader().getCity().equals("Milan"));
        System.out.println(b);
    }

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

        System.out.println("---------------------------------------");
        Integer sum2 = transactions.stream()
                .filter(t -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .mapToInt(t -> t).sum();
//                .collect(Collectors.summingInt(t -> t));
        System.out.println(sum2);

    }

    //7.所有交易中,最高的交易额是多少?
    @Test
    public void test7(){
        OptionalInt max = transactions.stream()
                .map(Transaction::getValue)
                .mapToInt(t -> t).max();
        System.out.println(max.getAsInt());

        System.out.println("-----------------------");
        Optional<Integer> max1 = transactions.stream()
                .map(Transaction::getValue)
                .max(Integer::compare);
        System.out.println(max1.get());
    }

    //8.找到交易额最小的交易
    @Test
    public void test8(){
        OptionalInt min = transactions.stream()
                .map(Transaction::getValue)
                .mapToInt(t -> t).min();
        transactions.stream()
                .filter(t -> t.getValue() <= min.getAsInt())
                .forEach(System.out::println);

        System.out.println("---------------------");
        Optional<Transaction> min1 = transactions.stream()
                .min(Comparator.comparingInt(Transaction::getValue));
//                .min(Comparator.comparingInt(x -> x.getValue()));
//                .min((x,y) -> Integer.compare(x.getValue(),y.getValue()));
        System.out.println(min1.get());

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值