最近在看《Java 8 in Action》,動手做了下5.5節(jié)的題目者蠕,Stream
果然是一把利器陋率,是時候升級到Java 8了。
題目:
1. Find all transactions in the year 2011 and sort them by value (small to high).
2. What are all the unique cities where the traders work?
3. Find all traders from Cambridge and sort them by name.
4. Return a string of all traders’ names sorted alphabetically.
5. Are any traders based in Milan?
6. Print all transactions’ values from the traders living in Cambridge.
7. What’s the highest value of all the transactions?
8. Find the transaction with the smallest value.
代碼:
package java8.ch_five;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
/**
* 答(非標(biāo)準(zhǔn)答案)
* @param args
*/
public static void main(String[] args) {
List<Transaction> transactions = buildDomains();
//1. Find all transactions in the year 2011 and sort them by value (small to high).
List<Transaction> list1 = transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted((t1, t2) -> t1.getValue() - t2.getValue())
.collect(Collectors.toList());
//2. What are all the unique cities where the traders work?
List<String> list2 = transactions.stream()
.map(Transaction::getTrader)
.map(Trader::getCity)
.distinct()
.collect(Collectors.toList());
//3. Find all traders from Cambridge and sort them by name.
List<Trader> list3 = transactions.stream()
.map(Transaction::getTrader)
.filter(t -> t.getCity().equals("Cambridge"))
.sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
.collect(Collectors.toList());
//4. Return a string of all traders’ names sorted alphabetically.
String allTradersName = transactions.stream()
.map(Transaction::getTrader)
.map(Trader::getName)
.sorted()
.reduce((a, b) -> a + "-" + b)
.orElse("");
//5. Are any traders based in Milan?
boolean hasTraderInMilan = transactions.stream()
.anyMatch(t -> t.getTrader().getCity().equals("Milan"));
//6. Print all transactions’ values from the traders living in Cambridge.
List<Integer> list6 = transactions.stream()
.filter(t -> t.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getValue)
.collect(Collectors.toList());
//7. What’s the highest value of all the transactions?
Integer maxValue = transactions.stream()
.map(Transaction::getValue)
.reduce(Integer::max)
.get();
//8. Find the transaction with the smallest value.
Transaction transaction = transactions.stream()
.reduce((a,b) -> a.getValue() < b.getValue() ? a : b)
.get();
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(allTradersName);
System.out.println(hasTraderInMilan);
System.out.println(list6);
System.out.println(maxValue);
System.out.println(transaction);
}
/**
* 標(biāo)準(zhǔn)答案
*/
public static void standardAnswer() {
List<Transaction> transactions = buildDomains();
//1. Find all transactions in the year 2011 and sort them by value (small to high).
List<Transaction> list1 = transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(Collectors.toList());
//2. What are all the unique cities where the traders work?
List<String> list2 = transactions.stream()
.map(t -> t.getTrader().getCity())
.distinct()
.collect(Collectors.toList());
//3. Find all traders from Cambridge and sort them by name.
List<Trader> list3 = transactions.stream()
.map(Transaction::getTrader)
.filter(t -> t.getCity().equals("Cambridge"))
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.collect(Collectors.toList());
//4. Return a string of all traders’ names sorted alphabetically.
String allTradersName = transactions.stream()
.map(t -> t.getTrader().getName())
.sorted()
.reduce("", (a, b) -> a + b);
//5. Are any traders based in Milan?
boolean hasTraderInMilan = transactions.stream()
.anyMatch(t -> t.getTrader().getCity().equals("Milan"));
//6. Print all transactions’ values from the traders living in Cambridge.
transactions.stream()
.filter(t -> t.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getValue)
.forEach(System.out::println);
//7. What’s the highest value of all the transactions?
Integer maxValue = transactions.stream()
.map(Transaction::getValue)
.reduce(Integer::max)
.get();
//8. Find the transaction with the smallest value.
Transaction transaction = transactions.stream()
.reduce((a,b) -> a.getValue() < b.getValue() ? a : b)
.get();
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(allTradersName);
System.out.println(hasTraderInMilan);
System.out.println(maxValue);
System.out.println(transaction);
}
private static List<Transaction> buildDomains() {
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)
);
return transactions;
}
}
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;
}
}
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 + "}";
}
}