Java 8 - Stream实战
作者:互联网
文章目录
Pre
都学了这俩,是不是该出来练一练了?
练习基础数据
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<Transaction> 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年发生的所有交易,并按交易额排序(从低到高)
List<Transaction> collect = transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(toList());
(2) 交易员都在哪些不同的城市工作过?
List<String> collect1 = transactions.stream()
.map(t->t.getTrader().getCity())
.distinct()
.collect(toList());
(3) 查找所有来自于剑桥的交易员,并按姓名排序。
transactions.stream().map(t->t.getTrader())
.filter(x -> x.getCity().equals("Cambridge"))
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.forEach(System.out::println);
(4) 返回所有交易员的姓名字符串,按字母顺序排序。
String value = transactions.stream().map(t -> t.getTrader().getName())
.distinct()
.sorted()
.reduce("", (name1, name2) -> name1 + name2);
System.out.println(value);
请注意,此解决方案效率不高(所有字符串都被反复连接,每次迭代的时候都要建立一个新的 String 对象)。
更为高效的解决方案,它像下面这样使用 joining
String traderStr =
transactions.stream()
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted()
.collect(joining());
(5) 有没有交易员是在米兰工作的?
boolean liveInMilan1 = transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan"));
boolean liveInMilan2 = transactions.stream().map(Transaction::getTrader).anyMatch(t -> t.getCity().equals("Milan"));
System.out.println(liveInMilan1);
System.out.println(liveInMilan2);
(6) 打印生活在剑桥的交易员的所有交易额。
transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getValue)
.forEach(System.out::println);
(7) 所有交易中,最高的交易额是多少?
Optional<Integer> maxValue = transactions.stream().map(Transaction::getValue).reduce((i, j) -> i > j ? i : j);
System.out.println(maxValue.get());
(8) 找到交易额最小的交易
Optional<Integer> minValue = transactions.stream().map(Transaction::getValue).reduce(Integer::min);
System.out.println(minValue.get());
还可以做得更好。流支持 min 和 max 方法,它们可以接受一个 Comparator 作为参数,指定计算最小或最大值时要比较哪个键值:
Optional<Transaction> smallestTransaction =
transactions.stream()
.min(comparing(Transaction::getValue));
附 Trader & Transaction
package com.artisan.java8.stream.excise;
public class Trader{
private final String name;
private final String city;
public Trader(String n, String c){
this.name = n;
this.city = c;
}
public String getName(){
return this.name;
}
public String getCity(){
return this.city;
}
public String toString(){
return "Trader:"+this.name + " in " + this.city;
}
}
package com.artisan.java8.stream.excise;
public class Transaction {
private final Trader trader;
private final int year;
private final int value;
public Transaction(Trader trader, int year, int value) {
this.trader = trader;
this.year = year;
this.value = value;
}
public Trader getTrader() {
return this.trader;
}
public int getYear() {
return this.year;
}
public int getValue() {
return this.value;
}
public String toString() {
return "{" + this.trader + ", " +
"year: " + this.year + ", " +
"value:" + this.value + "}";
}
}
标签:实战,Transaction,Java,Stream,stream,transactions,Trader,public,String 来源: https://blog.51cto.com/u_15239532/2835726