Java 8 - Stream流骚操作解读
Java 8 - Stream流骚操作解读2_归约操作
都学了这俩,是不是该出来练一练了?
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)
);
List<Transaction> collect = transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(toList());
List<String> collect1 = transactions.stream()
.map(t->t.getTrader().getCity())
.distinct()
.collect(toList());
transactions.stream().map(t->t.getTrader())
.filter(x -> x.getCity().equals("Cambridge"))
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.forEach(System.out::println);
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());
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);
transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getValue)
.forEach(System.out::println);
Optional<Integer> maxValue = transactions.stream().map(Transaction::getValue).reduce((i, j) -> i > j ? i : j);
System.out.println(maxValue.get());
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));
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 + "}";
}
}