public class DealTrans {
public static void main(String[] args) {
Trader raoul = new Trader("Raoul", "Cambridge");
Trader mario = new Trader("Mario", "Milan");
Trader alan = new Trader("Alan", "Cambridge");
Trader brain = new Trader("Brain", "Cambridge");
List<Transaction> transactions = Arrays.asList(
new Transaction(brain, 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)
);
DealTrans dealTrans = new DealTrans();
List<Transaction> list1 = dealTrans.function1(transactions);
System.out.println("1------------------------------:");
list1.forEach(System.out::println);
List<String> list2 = dealTrans.function2(transactions);
System.out.println("2------------------------------:");
list2.forEach(System.out::println);
List<Trader> list3 = dealTrans.function3(transactions);
System.out.println("3------------------------------:");
list3.forEach(System.out::println);
List<String> list4 = dealTrans.function4(transactions);
System.out.println("4------------------------------:");
list4.forEach(System.out::println);
boolean b5 = dealTrans.function5(transactions);
System.out.println("5------------------------------:");
System.out.println(b5);
dealTrans.function6(transactions);
int i7 = dealTrans.function7(transactions);
System.out.println("7------------------------------:");
System.out.println("Max:" + i7);
Transaction t8 = dealTrans.function8(transactions);
System.out.println("8------------------------------:");
System.out.println(t8.toString());
}
/**
* (1) 找出2011年发生的所有交易,并按交易额排序(从低到高)。
* @param allTrans
* @return
*/
public List<Transaction> function1(List<Transaction> allTrans){
return allTrans.stream()
.filter(t -> t.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(toList());
}
/**
* (2) 交易员都在哪些不同的城市工作过?
* @param allTrans
* @return
*/
public List<String> function2(List<Transaction> allTrans){
return allTrans.stream()
.map(t -> t.getTrader().getCity())
.distinct()
.collect(toList());
}
/**
* (3) 查找所有来自于剑桥的交易员,并按姓名排序。
* @param allTrans
* @return
*/
public List<Trader> function3(List<Transaction> allTrans){
return allTrans.stream()
.map(Transaction::getTrader)
.filter(trader -> "Cambridge".equals(trader.getCity()))
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.collect(toList());
}
/**
* (4)返回所有交易员的姓名字符串,按字母顺序排序。
* @param allTrans
* @return
*/
public List<String> function4(List<Transaction> allTrans){
return allTrans.stream()
.map(t -> t.getTrader().getName())
.distinct()
.sorted()
.collect(toList());
}
/**
* (5) 有没有交易员是在米兰工作的?
* @param allTrans
* @return
*/
public boolean function5(List<Transaction> allTrans){
return allTrans.stream()
.anyMatch(transaction -> "Milan".equals(transaction.getTrader().getCity()));
}
/**
* (6) 打印生活在剑桥的交易员的所有交易额。
* @param allTrans
*/
public void function6(List<Transaction> allTrans){
System.out.println("6------------------------------:");
allTrans.stream()
.filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
.map(Transaction::getValue)
.forEach(System.out::println);
}
/**
* (7) 所有交易中,最高的交易额是多少?
* @param allTrans
* @return
*/
public int function7(List<Transaction> allTrans){
return allTrans.stream()
.map(Transaction::getValue)
.reduce(0, Integer::max);
}
/**
* (8) 找到交易额最小的交易。
* @param allTrans
* @return
*/
public Transaction function8(List<Transaction> allTrans){
return allTrans.stream()
.min(Comparator.comparing(Transaction::getValue))
.orElse(null);
}
}
网友评论