Stream流练习

package com.example.yj.practice;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.example.yj.entity.Employees;
import com.example.yj.entity.Trader;
import com.example.yj.entity.Transaction;
import org.junit.Test;

/**

  • 作者:果果

  • 时间:2022/5/31 10:04
    /
    public class MyPracticeApi {
    /

    1.给定一组数字,返回由每个数字平方构成的列表
    ,给定[1,2,3,4,5] 应该返回 [1,4,9,16,25]
    */

    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 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)
    );

    @Test
    public void test01() {
    List integers = Arrays.asList(1, 2, 3, 4, 5);
    List collect = integers.stream()
    .map(x -> x * x)
    .collect(Collectors.toList());
    System.out.println(collect);
    }

    /*
    2.咋样用Map 和 reduce 方法数一数流中 有多少个Employee呢?
    */
    @Test
    public void test02() {
    List employees = Arrays.asList(
    new Employees("马海超", "18", "13797706270", Employees.Status.FREE),
    new Employees("刘振宇", "21", "18571456081", Employees.Status.FREE),
    new Employees("吴建刚", "31", "13477300996", Employees.Status.FREE),
    new Employees("刘振宇", "31", "13477300996", Employees.Status.FREE),
    new Employees("刘振宇", "31", "13477300996", Employees.Status.FREE),
    new Employees("刘振宇", "31", "13477300996", Employees.Status.FREE)
    );
    Optional reduce = employees.stream()
    .map((x) -> 1)
    .reduce(Integer::sum);
    System.out.println(reduce.get());

    }

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

    }

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

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

    //4.返回所有交易员的姓名字符串,按字母顺序排序
    @Test
    public void test06(){
    transactions.stream()
    .map((x) -> x.getTrader().getName())
    .flatMap(MyPracticeApi::getChar)
    .sorted((x1,x2) -> x1.compareToIgnoreCase(x2))
    .forEach(System.out::print);

    }
    public static Stream getChar(String str){
    ArrayList list = new ArrayList<>();
    for(Character c:str.toCharArray()){
    list.add(c.toString());
    }
    return list.stream();
    }

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

    }

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

    //7.所有交易中,最高的交易额是多少
    @Test
    public void test09_1(){
    Optional max = transactions.stream()
    .map((x) -> x.getMoney())
    .max(Integer::compare);
    System.out.println(max.get());
    }

    //8.找到交易额最小的交易
    @Test
    public void test10(){
    Optional min = transactions.stream()
    .min((x, y) -> Integer.compare(x.getMoney(), x.getMoney()));
    System.out.println(min.get());

    }
    }

posted @   老男人伍六七  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示