摘要:交易員練習(xí)年的所有交易并按照金額由小到大排序交易員都在哪些不同的城市生活查找所有來(lái)自劍橋的交易員,并按姓名排序劍橋查詢所有交易員的姓名字符串,并按字母排序有沒有交易員在米蘭米蘭打印在劍橋生活的交易員的所有交易金額劍橋所有交易中,
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交易員練習(xí) */ 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.查找所有來(lái)自劍橋的交易員,并按姓名排序 @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.統(tǒng)計(jì)每個(gè)交易員的記錄 @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.統(tǒng)計(jì)交易總額最高的交易員(排序) @Test public void exercise11(){ transactions.stream() .collect(groupingBy(Transaction :: getTrader)) .entrySet().stream() .map( t -> { Mapresult = 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.使用方法引用對(duì)transaction排序 @Test public void exercise12(){ transactions.stream() .sorted(Comparator.comparing(Transaction :: getValue)) .forEach(System.out :: println); System.out.println("------------------------------------------"); //聲明接口的的實(shí)現(xiàn)方式 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.根據(jù)trader(對(duì)象)將transaction分組 @Test public void exercise13(){ transactions.stream() .collect(groupingBy(Transaction :: getTrader)) .entrySet().stream() .forEach(System.out :: println); } //【14.根據(jù)貨幣(字符串)類型分組 @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轉(zhuǎn)換求和sum1 = " + sum1); int sum2 = transactions.stream().collect(Collectors.summingInt(Transaction::getValue)); System.out.println("通過collect匯總求和sum2 = " + sum2); //規(guī)約操作都需要使用map將對(duì)象映射為返回值的類型 int sum3 = transactions.stream().map(Transaction::getValue).reduce(0,(t1,t2) -> t1 + t2); System.out.println("通過reduce規(guī)約求和sum3 = " + sum3); } //【16.二級(jí)分類,先按照交易員分類,然后交易金額大于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.獲取每個(gè)交易員,交易最大的一筆 @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); } //===================================初始化數(shù)據(jù)================================ 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)); } //交易員對(duì)象 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; } } //交易對(duì)象 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; } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/68144.html
摘要:寫在前面作為常年與服務(wù)器打交道的后端開發(fā),基本的操作是一定要運(yùn)用非常熟練的本篇文章就記錄了一些日常工作中最常用的的指令,希望能和大家共同學(xué)習(xí)共同進(jìn)步一與的區(qū)別是的升級(jí)版本,它兼容的所有指令,并提供一些新特性,如以不同顏色標(biāo)識(shí)語(yǔ)法等之后會(huì)總結(jié) 寫在前面:作為常年與服務(wù)器、Linux打交道的后端開發(fā)RD,基本的vi操作是一定要運(yùn)用非常熟練的;本篇文章就記錄了一些日常工作中最常用的的指令,希...
摘要:命令模式需要先輸入冒號(hào),才會(huì)進(jìn)入。上下左右左右下上下一個(gè)詞,上一個(gè)詞常用下一個(gè)詞。如果要取消這種縮進(jìn)的話,就要進(jìn)入到粘貼模式記得在這個(gè)模式下,無(wú)法使用命令來(lái)快速打開文件。 Vim三種模式:(重要) 導(dǎo)航(navigation)模式: 這時(shí)候,字母就是上下左右鍵。輸入模式:你按字母鍵,才會(huì)輸入字母。命令模式:需要先輸入: 冒號(hào),才會(huì)進(jìn)入。例如,你輸入 :ls , 就相當(dāng)于運(yùn)行了 ls...
摘要:下面從這幾個(gè)方面用到的命令進(jìn)行闡述模式切換常用按鍵塊選擇多窗口操作功能模式切換有三種模式為一般模式,編輯模式,命令行模式。,將我們當(dāng)前打開的文件劃分為多個(gè)窗口移動(dòng)到上面窗口移動(dòng)到下面窗口退出當(dāng)前窗口以上為我們?cè)谑褂弥谐S玫降囊恍┟畈僮鳌? 對(duì)于Vi的學(xué)習(xí),在這里算是做個(gè)筆記,對(duì)于一些常用的命令記錄下,以后在使用起來(lái)會(huì)更方便,便于以后查閱使用,而不需要再?gòu)娜ニ阉鳌Wx到這你應(yīng)該看出,這是一...
閱讀 1439·2021-11-11 16:54
閱讀 9319·2021-11-02 14:44
閱讀 2371·2021-10-22 09:53
閱讀 3259·2019-08-30 11:18
閱讀 1951·2019-08-29 13:29
閱讀 2003·2019-08-27 10:58
閱讀 1623·2019-08-26 11:38
閱讀 3518·2019-08-26 10:31