Stream中间操作小练习

Stream中间操作小练习

给定一个数字列表,如何返回一个由每个数的平方构成的列表呢

    /**
     * 给定一个数字列表,如何返回一个由每个数的平方构成的列表呢
     * eg: 1 2 3 4 5
     * 返回 1 4 9 16 25
     */
    @Test
    public void test1() {
        List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
        nums.stream().map(x -> x * x).collect(Collectors.toList()).forEach(System.out::println);
    }
   

map与reduce结合计算Employee中的个数

实体类

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee {
    private String name;
    private Integer age;
    private Double salary;
    private Status status;

    public Employee(Integer age){
        this.age = age;
    }

    public Employee(Integer age, Double salary){
        this.age = age;
        this.salary = salary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name) &&
                Objects.equals(age, employee.age) &&
                Objects.equals(salary, employee.salary) &&
                status == employee.status;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary, status);
    }

    public enum Status{
        FREE,
        BUSY,
        VOCATION
    }
}

 /**
     * 怎样用map和reduce方法数一数流中有多少个Employee?
     */
    @Test
    public void test2(){
        List<Employee> employees = Arrays.asList(
                new Employee("张三", 18, 9999.99, Employee.Status.FREE),
                new Employee("李四", 48, 3333.99,Employee.Status.BUSY),
                new Employee("王五", 38, 5999.99, Employee.Status.VOCATION),
                new Employee("赵六", 38, 5999.99, Employee.Status.BUSY),
                new Employee("田七", 8, 6999.99, Employee.Status.VOCATION)
        );
        Optional<Integer> reduce = employees.stream().map((e) -> 1).reduce(Integer::sum);
        System.out.println(reduce.get());
    }

根据如下条件找出符合条件的记录数

1.找出2011年发生的所有交易,并按交易额排序(从低到高)
2.交易员都在哪些不同的城市工作过
3.查找所有来自剑桥的交易员,并按照姓名排序
4.返回所有交易员的姓名字符串,按照字母顺序排序
5.有没有交易员是在米兰工作的
6.打印生活在剑桥的交易员的所有交易额
7.所有交易中,最高的交易额是多少
8.找到交易额最小的交易

实体类


/**
 * 交易员
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Trader {
    private String name;
    private String city;
}

/**
 * 交易类
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Transaction {
    private Trader trader;
    private int year;
    private int value;
}

测试类

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

    }
    /*public static Stream<String> getCity(List<Trader> traders){
        List<String> list = new ArrayList<>();
        for(Trader trader:traders){
            list.add(trader.getCity());
        }
        return list.stream();
    }*/
    //查找所有来自剑桥的交易员,并按照姓名排序
    @Test
    public void test3(){
        transactions.stream()
                .map(Transaction::getTrader)
                .filter(x->x.getCity().equals("Cambridge"))
                .sorted((x,y)->x.getName().compareTo(y.getName()))
                .distinct()
                .forEach(System.out::println);
        System.out.println("----------------------------------------------");
        transactions.stream()
                .filter(x->x.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .sorted((x,y)->x.getName().compareTo(y.getName()))
                .distinct()
                .forEach(System.out::println);
    }
    //返回所有交易员的姓名字符串,按照字母顺序排序
    @Test
    public void test4(){
        transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getName)
                .sorted()
                .forEach(System.out::println);
        System.out.println("--------------------------------------");
        transactions.stream()
                .map((t)->t.getTrader().getName())
                .sorted()
                .forEach(System.out::println);
        System.out.println("--------------------------------------");
        String reduce = transactions.stream()
                .map((t) -> t.getTrader().getName())
                .sorted()
                .reduce("", String::concat);
        System.out.println(reduce);
        System.out.println("--------------------------------------");
        transactions.stream()
                .map((t)->t.getTrader().getName())
                .flatMap(TestTransaction::filterCharacter)
                .sorted((s1,s2)->s1.compareToIgnoreCase(s2))
                .forEach(System.out::print);

    }
    public static Stream<String> filterCharacter(String str){
        List<String> list = new ArrayList<>();
        for(Character ch:str.toCharArray()){
            list.add(ch.toString());
        }
        return list.stream();
    }
    //有没有交易员是在米兰工作的
    @Test
    public void test5(){
        boolean milan = transactions.stream()
                .map(Transaction::getTrader)
                .anyMatch(x -> x.getCity().equals("Milan"));
        System.out.println(milan);
        System.out.println("------------------------------------------");
        boolean b = transactions.stream()
                .anyMatch(x -> x.getTrader().getCity().equals("Milan"));
        System.out.println(b);
    }
    //打印生活在剑桥的交易员的所有交易额
    @Test
    public void test6(){
        Optional<Integer> sum = transactions.stream()
                .filter((x) -> x.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);
        System.out.println(sum.get());

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

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值