所需数据
Trader
package com.qianfeng.homework;
public class Trader {
private String name;
private String city;
public Trader() {
}
public Trader(String name, String city) {
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Trader{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Trader trader = (Trader) o;
if (!name.equals(trader.name)) return false;
return city.equals(trader.city);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + city.hashCode();
return result;
}
}
Transaction
package com.qianfeng.homework;
public class Transaction {
private Trader trader;
private int value;
private int currency;
public Transaction() {
}
public Transaction(Trader trader, int value, int currency) {
this.trader = trader;
this.value = value;
this.currency = currency;
}
public Trader getTrader() {
return trader;
}
public void setTrader(Trader trader) {
this.trader = trader;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getCurrency() {
return currency;
}
public void setCurrency(int currency) {
this.currency = currency;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Transaction that = (Transaction) o;
if (value != that.value) return false;
if (currency != that.currency) return false;
return trader.equals(that.trader);
}
@Override
public int hashCode() {
int result = trader.hashCode();
result = 31 * result + value;
result = 31 * result + currency;
return result;
}
@Override
public String toString() {
return "Transaction{" +
"trader=" + trader +
", value=" + value +
", currency=" + currency +
'}';
}
}
数据结构
练习题
注意stream流只能使用一次,下面的每个题目公用的一个流是不对的,只是为了方便,真正做的时候要注释掉其他的题目代码
package com.qianfeng.homework;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Work01 {
//数据准备
static List<Transaction> transactions;
static {
Trader raoul = new Trader("Raoul", "Cambridge");
Trader mario = new Trader("Mario", "Milan");
Trader alan = new Trader("Alan", "Cambridge");
Trader brian = new Trader("Brian", "Cambridge");
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)
);
}
public static void main(String[] args) {
System.out.println(transactions);
Stream<Transaction> transactionStream = transactions.stream();
// 1.找出2011年发生的所有交易,并按交易额排序降序
List<Transaction> collect = transactionStream
.filter(transaction -> transaction.getValue() == 2011)
.sorted((transaction1, transaction2) -> transaction2.getCurrency() - transaction2.getCurrency())
.collect(Collectors.toList());
System.out.println(collect);
// 2.交易员都在哪些不同的城市工作过?不显示重复数据
List<String> collect = transactionStream
.map(transaction -> transaction.getTrader()
.getCity()).distinct().collect(Collectors.toList());
System.out.println(collect);
// 3.查找所有来自剑桥的交易员,并且按照姓名排序
List<Transaction> collect = transactionStream
.filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
.sorted((transaction1, transaction2) -> transaction1.getTrader().getName().compareTo(transaction2.getTrader().getName()))
.collect(Collectors.toList());
System.out.println(collect);
// 4.返回所有交易员的姓名字符,按照字母排序 返回数据格式String[]
String[] strings = transactionStream
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted(String::compareTo)
.toArray(String[]::new);
System.out.println(Arrays.toString(strings));*/
// 4-1.返回所有交易员的姓名字符, 按照字母排序 返回数据格式List<String>
List<String> collect = transactionStream
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted(String::compareTo)
.collect(Collectors.toCollection(ArrayList<String>::new));
System.out.println(collect);
//
// 4-2.返回所有交易员的姓名字符, 按照字母排序 返回数据格式Set<String>
HashSet<String> collect = transactionStream
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted(String::compareTo)
.collect(Collectors.toCollection(HashSet<String>::new));
System.out.println(collect);
// 5.有没有交易员是在Milan工作的? 返回交易员集合
// 对象去重要重写hashcode 和 equals
Optional<List<Trader>> optional = Optional.ofNullable(transactionStream
.filter(transaction -> transaction.getTrader().getCity().equals("Milan"))
.map(Transaction::getTrader).distinct().collect(Collectors.toList()));
if (optional.isPresent()) {
System.out.println(optional.get());
}else {
System.out.println("不存在");
}
// 5-1.有没有交易员是在Milan工作的? 返回交易员姓名集合
Optional<List<String>> optional = Optional.ofNullable(transactionStream
.filter(transaction -> transaction.getTrader().getCity().equals("Milan"))
.map(transaction -> transaction.getTrader().getName()).distinct().collect(Collectors.toList()));
if (optional.isPresent()) {
System.out.println(optional.get());
}else {
System.out.println("不存在");
}
// 5-2.有没有交易员是在Milan工作的? 简单判断返回 true/false
boolean anyMatch = transactionStream.anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
System.out.println(anyMatch);
// 6.打印交易员的所有交易额
int sum = transactionStream.mapToInt(Transaction::getCurrency).sum();
System.out.println(sum);
// 6-1.打印在 [剑桥] 的交易员的所有交易额
int sum = transactionStream
.filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
.mapToInt(Transaction::getCurrency).sum();
System.out.println(sum);
// 或者
System.out.println(transactionStream
.mapToInt(Transaction::getCurrency)
.reduce(0, (cu1, cu2) -> cu1 + cu2));
// 7.所有交易中,打印 所有数据中的 交易额最高的是多少
System.out.println(transactionStream.mapToInt(Transaction::getCurrency).max().getAsInt());
// 7-1.所有数据中的 交易额最小值,交易额平均值,交易次数
System.out.println(transactionStream.mapToInt(Transaction::getCurrency).min().getAsInt());
System.out.println(transactionStream.mapToInt(Transaction::getCurrency).average().getAsDouble());
System.out.println(transactionStream.mapToInt(Transaction::getCurrency).count());
// 按照区域分组
Map<String, List<Transaction>> collect = transactionStream
.collect(Collectors.groupingBy(transaction -> transaction.getTrader().getCity()));
//打印
collect.forEach((k,v)->{
System.out.println(k+":");
v.forEach((transaction)->{
System.out.println("\t"+transaction);
});
});
// 先按照区域分组,再按照value分组
Map<String, Map<Integer, List<Transaction>>> collect = transactionStream
.collect(Collectors.groupingBy(transaction -> transaction.getTrader().getCity(),
Collectors.groupingBy(transaction -> transaction.getValue())));
collect.forEach((k,v)->{
System.out.println(k+":");
v.forEach((k2,v2)->{
System.out.println("\t"+k2+":");
System.out.println("\t\t"+v2);
});
});
// 二级分组,并统计总金额
Map<String, Map<Integer, Integer>> collect = transactionStream
.collect(Collectors.groupingBy(transaction -> transaction.getTrader().getCity(),
Collectors.groupingBy(transaction -> transaction.getValue(), Collectors.summingInt(Transaction::getCurrency))));
//打印
collect.forEach((k,v)->{
System.out.println(k+":");
v.forEach((k2,v2)->{
System.out.println("\t"+k2+":");
System.out.println("\t\t"+v2);
});
});
}
}