资讯专栏INFORMATION COLUMN

java8-stream常用操作

Jokcy / 310人阅读

摘要:交易员练习年的所有交易并按照金额由小到大排序交易员都在哪些不同的城市生活查找所有来自剑桥的交易员,并按姓名排序剑桥查询所有交易员的姓名字符串,并按字母排序有没有交易员在米兰米兰打印在剑桥生活的交易员的所有交易金额剑桥所有交易中,

import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.*;

/**
 * Created by ibm on 2017/4/12.
 * java8交易员练习
 */
public class TraderExercise {

    //【1.2011年的所有交易并按照金额由小到大排序
    @Test
    public void exercise1(){
        transactions.stream()
                .filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .forEach(System.out :: println);
    }
    //【2.交易员都在哪些不同的城市生活
    @Test
    public void exercise2(){
        traders.stream()
                .map(Trader :: getCity)
                .distinct()
                .forEach(System.out :: println);
    }
    //【3.查找所有来自剑桥的交易员,并按姓名排序
    @Test
    public void exercise3(){
        traders.stream()
                .filter( t -> "剑桥".equals(t.getCity()))
                .sorted(Comparator.comparing(Trader :: getName))
                .forEach(System.out :: println);
    }
    //【4.查询所有交易员的姓名字符串,并按字母排序
    @Test
    public void exercise4(){
        traders.stream()
                .sorted(Comparator.comparing(Trader :: getName).reversed())
                .forEach(t -> System.out.println(t.getName()));
    }
    //【5.有没有交易员在米兰
    @Test
    public void exercise5(){
        traders.stream()
                .filter(t -> "米兰".equals(t.getCity()))
                .findAny()
                .ifPresent(System.out :: println);
    }
    //【6.打印在剑桥生活的交易员的所有交易金额
    @Test
    public void exercise6(){
        int sumValue = transactions.stream()
                .filter(tran -> "剑桥".equals(tran.getTrader().getCity()))
                .map(Transaction::getValue)
                .reduce(0,(value1 , value2) -> value1 + value2);
        System.out.println(sumValue);
    }
    //【7.所有交易中,最高的交易额是多少
    @Test
    public void exercise7(){
         transactions.stream()
                .sorted(Comparator.comparing(Transaction :: getValue).reversed())
                .findFirst()
                .ifPresent(System.out :: println);

         transactions.stream()
                .map(Transaction :: getValue)
                .reduce(Integer :: max)
                .ifPresent(System.out :: println);
    }
    //【8.找到交易额中最小的金额
    @Test
    public void exercise8(){
        transactions.stream()
                .map(Transaction :: getValue)
                .reduce(Integer :: min)
                .ifPresent(System.out :: println);

        transactions.stream()
                .min(Comparator.comparing(Transaction :: getValue))
                .ifPresent(System.out :: println);

        transactions.stream()
                .min(Comparator.comparing((Transaction t1) -> t1.getValue()))
                .ifPresent(System.out :: println);
    }
    //【9.统计每个交易员的记录
    @Test
    public void exercise9(){
        transactions.stream()
                .collect(groupingBy(Transaction :: getTrader))
                .entrySet().stream()
                .forEach(System.out :: println);
    }
    //【10.找到单笔交易最高的交易员
    @Test
    public void exercise(){
        transactions.stream()
                .max(Comparator.comparing(Transaction :: getValue))
                .ifPresent( tran -> {
                    System.out.println(tran.getTrader());
                } );
    }
    //【11.统计交易总额最高的交易员(排序)
    @Test
    public void exercise11(){
        transactions.stream()
                .collect(groupingBy(Transaction :: getTrader))
                .entrySet().stream()
                .map( t -> {
                    Map result = new HashMap<>();
                    int sum = t.getValue().stream().mapToInt(Transaction :: getValue).sum();
                    result.put("sum",sum);
                    result.put("trader",t.getKey());
                    return result;
                })
                .sorted(comparingInt((Map m) -> (int)m.get("sum")).reversed())
                .findFirst().ifPresent(System.out::println);
    }
    //【12.使用方法引用对transaction排序
    @Test
    public void exercise12(){
        transactions.stream()
                .sorted(Comparator.comparing(Transaction :: getValue))
                .forEach(System.out :: println);

        System.out.println("------------------------------------------");
        //声明接口的的实现方式
        Function function = Transaction :: getValue;
        Transaction[] transactionsArray = new Transaction[transactions.size()];
        Arrays.sort(transactions.toArray(transactionsArray),Comparator.comparing(function));
        Arrays.asList(transactionsArray).stream().forEach(System.out :: println);
    }
    //【13.根据trader(对象)将transaction分组
    @Test
    public void exercise13(){
        transactions.stream()
                .collect(groupingBy(Transaction :: getTrader))
                .entrySet().stream()
                .forEach(System.out :: println);
    }
    //【14.根据货币(字符串)类型分组
    @Test
    public void exercise14(){
        transactions.stream()
                .collect(groupingBy(Transaction :: getCurrency))
                .entrySet().stream()
                .forEach(System.out :: println);
    }
    //【15.获取交易总金额
    @Test
    public void exercise15(){
        int sum1 = transactions.stream().mapToInt(Transaction::getValue).sum();
        System.out.println("通过map转换求和sum1 = " + sum1);
        int sum2 = transactions.stream().collect(Collectors.summingInt(Transaction::getValue));
        System.out.println("通过collect汇总求和sum2 = " + sum2);
        //规约操作都需要使用map将对象映射为返回值的类型
        int sum3 = transactions.stream().map(Transaction::getValue).reduce(0,(t1,t2) -> t1 + t2);
        System.out.println("通过reduce规约求和sum3 = " + sum3);
    }
    //【16.二级分类,先按照交易员分类,然后交易金额大于800归为high,低于800归为low
    @Test
    public void exercise16(){
        transactions.stream()
                .collect(groupingBy(Transaction :: getTrader,groupingBy(t -> {
                    if(t.getValue()< 800 ){
                        return "low";
                    }else {
                        return "heigh";
                    }
                })))
                .entrySet().stream()
                .forEach(System.out :: println);
    }
    //【17.获取每个交易员,交易最大的一笔
    @Test
    public void exercise17(){
        transactions.stream()
                .collect(groupingBy(Transaction::getTrader,maxBy(Comparator.comparingInt(Transaction::getValue))))
                .entrySet().stream()
                .distinct()
                .sorted(Comparator.comparing((Map.Entry m) -> {
                    Trader t = (Trader) m.getKey();
                    return t.getName();
                }))
                .forEach(System.out :: println);
    }

    //===================================初始化数据================================
    List traders = new LinkedList<>();
    List transactions = new LinkedList<>();

    public TraderExercise(){
        Trader t1 = new Trader("trader1","剑桥");
        Trader t2 = new Trader("trader2","米兰");
        Trader t3 = new Trader("trader3","剑桥");
        Trader t4 = new Trader("trader4","剑桥");
        traders.addAll(Arrays.asList(t1,t2,t3,t4));

        Transaction tran1 = new Transaction(t4,2011,300,"$");
        Transaction tran2 = new Transaction(t1,2012,1000,"$");
        Transaction tran3 = new Transaction(t1,2011,400,"¥");
        Transaction tran4 = new Transaction(t2,2012,710,"¥");
        Transaction tran5 = new Transaction(t2,2012,700,"$");
        Transaction tran6 = new Transaction(t3,2012,950,"¥");
        transactions.addAll(Arrays.asList(tran1,tran2,tran3,tran4,tran5,tran6));
    }

    //交易员对象
    class Trader{

        private String name;
        private String city;

        public Trader(String name,String city){
            this.name = name;
            this.city = city;
        }

        public String getName(){
            return name;
        }
        public String getCity(){
            return city;
        }

        @Override
        public String toString(){
           return "i am trader : " + name + " ; i live in : " + city;
        }
    }
    //交易对象
    class Transaction{

        private Trader trader;
        private int year;
        private int value;
        private String currency;

        public Transaction(Trader trader,int year,int value,String currency){
            this.trader = trader;
            this.year = year;
            this.value = value;
            this.currency = currency;
        }

        public Trader getTrader(){
            return trader;
        }
        public int getYear(){
            return year;
        }
        public int getValue(){
            return value;
        }
        public String getCurrency(){
            return currency;
        }

        @Override
        public String toString(){
            return "i am transaction : my trader is : "
                    + trader.getName()
                    + " ; my year is : "
                    + year
                    + " ; my value is : "
                    + value
                    + " ; my currency is : "
                    + currency;
        }
    }

}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68144.html

相关文章

  • RD基本功——Vi基础与最常用指令

    摘要:写在前面作为常年与服务器打交道的后端开发,基本的操作是一定要运用非常熟练的本篇文章就记录了一些日常工作中最常用的的指令,希望能和大家共同学习共同进步一与的区别是的升级版本,它兼容的所有指令,并提供一些新特性,如以不同颜色标识语法等之后会总结 写在前面:作为常年与服务器、Linux打交道的后端开发RD,基本的vi操作是一定要运用非常熟练的;本篇文章就记录了一些日常工作中最常用的的指令,希...

    G9YH 评论0 收藏0
  • Vim常用命令

    摘要:命令模式需要先输入冒号,才会进入。上下左右左右下上下一个词,上一个词常用下一个词。如果要取消这种缩进的话,就要进入到粘贴模式记得在这个模式下,无法使用命令来快速打开文件。 Vim三种模式:(重要) 导航(navigation)模式: 这时候,字母就是上下左右键。输入模式:你按字母键,才会输入字母。命令模式:需要先输入: 冒号,才会进入。例如,你输入 :ls , 就相当于运行了 ls...

    happyhuangjinjin 评论0 收藏0
  • Vim常用按键操作学习

    摘要:下面从这几个方面用到的命令进行阐述模式切换常用按键块选择多窗口操作功能模式切换有三种模式为一般模式,编辑模式,命令行模式。,将我们当前打开的文件划分为多个窗口移动到上面窗口移动到下面窗口退出当前窗口以上为我们在使用中常用到的一些命令操作。 对于Vi的学习,在这里算是做个笔记,对于一些常用的命令记录下,以后在使用起来会更方便,便于以后查阅使用,而不需要再从去搜索。读到这你应该看出,这是一...

    MSchumi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<