执行交易的交易员。你的经理让你为八个查询找到答案。
数据初始化
首先我们需要一个交易员类,提供交易员的信息:
package jdk8.jdk8.stream.practice;
public class Trader {
private final String name;
private final String city;
public Trader(String name, String city) {
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return "Trader{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}
然后我们再创建个交易类,与交易员有直接联系关系,该类用于提供交易信息:
package jdk8.jdk8.stream.practice;
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 trader;
}
public int getYear() {
return year;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return "Transaction{" +
"trader=" + trader +
", year=" + year +
", value=" + value +
'}';
}
}
我们再创建个类,对数据进行个初始化:
package jdk8.jdk8.stream.practice;
import java.util.Arrays;
import java.util.List;
public class init {
public init(){}
public static List<Transaction> transactions;
public static Trader raoul;
public static Trader mario;
public static Trader alan;
public static Trader brian;
static{
raoul = new Trader("Raoul", "Cambridge");
mario = new Trader("Mario","Milan");
alan = new Trader("Alan","Cambridge");
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)
);
}
}
随后我们就可以让练习主类中继承这个初始化类,这样就可以直接使用数据了(这样写主要是为了不让主类看着太杂),这里我用静态代码块初始化,随后我们可以直接用Class.forName类加载进行初始化。
需要处理的问题
(1) 找出2011年发生的所有交易,并按交易额排序(从低到高)。
这里我们先过滤出2011的所有交易,然后排序即可。
private static void practice01(){
transactions.stream()
.filter(transaction -> transaction.getYear()==2011)
.sorted(((o1, o2) -> o1.getValue()-o2.getValue()))
.forEach(transaction -> System.out.println(transaction.toString()));
}
(2) 交易员都在哪些不同的城市工作过?
这里我们把城市名称映射出来,然后再去重即可;
private static void practice02(){
transactions.stream()
.map(stransaction-> stransaction.getTrader().getCity())
.distinct()
.forEach(System.out::println);
}
(3) 查找所有来自于剑桥的交易员,并按姓名排序。
先过滤出是剑桥的交易员,然后记得去重,因为交易里有些交易员进行了多个交易,所以过滤交易员可能会有重复的交易员,最后再把姓名进行排序即可;
private static void practice03(){
transactions.stream()
.filter(transacion->transacion.getTrader().getCity()=="Cambridge")
.map(transaction->transaction.getTrader())
.distinct()
.sorted((o1,o2)->o1.getName().compareTo(o2.getName()))
.forEach(System.out::println);
}
(4) 返回所有交易员的姓名字符串,按字母顺序排序。
这里我们先将交易映射成交易员的姓名进行处理,然后去重(原因上一题说明了),最好排序即可;
private static void practice04(){
transactions.stream()
.map(transaction->transaction.getTrader().getName())
.distinct()
.sorted()
.forEach(System.out::println);
}
(5) 有没有交易员是在米兰工作的?
这里可以直接用anyMatch 进行交易员所在城市进行匹配即可;
private static void practice05(){
if(transactions.stream()
.anyMatch(transaction -> transaction.getTrader().getCity()=="Milan"))
System.out.println("存在交易员在Milan");
else
System.out.println("不存在交易员在Milan");
}
(6) 打印生活在剑桥的交易员的所有交易额。
首先咱先过滤出是交易员是剑桥的交易信息,然后再映射成交易金额,然后再用reduce 求和即可;
private static void practice06(){
Integer sum = transactions.stream()
.filter(transaction -> transaction.getTrader().getCity()=="Cambridge")
.map(transaction -> transaction.getValue())
.reduce(0,(num1,num2)->num1+num2);
System.out.println("Canbridge的所有交易额 = " + sum);
}
(7) 所有交易中,最高的交易额是多少?
我们把交易流映射成交易额,然后求其最大值就可以啦;
private static void practice07(){
transactions.stream()
.map(transaction -> transaction.getValue())
.reduce(Integer::max)
.ifPresent(System.out::println);
}
这里reduce 是终端方法,流操作此时已经结束了,返回的是一个Optional 对象,然后我们对该对象进行操作,对该对象里存的结果(value)进行输出。
(8) 找到交易额最小的交易。
与上题求最大交易是一样操作;
private static void practice08(){
transactions.stream()
.map(transaction -> transaction.getValue())
.reduce(Integer::min)
.ifPresent(System.out::println);
}
main代码
package jdk8.jdk8.stream.practice;
public class Text extends init{
public static void main(String[] args) {
try {
Class.forName("jdk8.jdk8.stream.practice.init");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
practice01();
System.out.println("-------------------------------");
practice02();
System.out.println("-------------------------------");
practice03();
System.out.println("-------------------------------");
practice04();
System.out.println("-------------------------------");
practice05();
System.out.println("-------------------------------");
practice06();
System.out.println("-------------------------------");
practice07();
System.out.println("-------------------------------");
practice08();
System.out.println("-------------------------------");
}
/**
* 找到交易额最小的交易。
*/
private static void practice08(){
transactions.stream()
.map(transaction -> transaction.getValue())
.reduce(Integer::min)
.ifPresent(System.out::println);
}
/**
* 所有交易中,最高的交易额是多少?
*/
private static void practice07(){
transactions.stream()
.map(transaction -> transaction.getValue())
.reduce(Integer::max)
.ifPresent(System.out::println);
}
/**
* 打印生活在剑桥的交易员的所有交易额。
*/
private static void practice06(){
Integer sum = transactions.stream()
.filter(transaction -> transaction.getTrader().getCity()=="Cambridge")
.map(transaction -> transaction.getValue())
.reduce(0,(num1,num2)->num1+num2);
System.out.println("Canbridge的所有交易额 = " + sum);
}
/**
* 有没有交易员是在米兰工作的?
*/
private static void practice05(){
if(transactions.stream()
.anyMatch(transaction -> transaction.getTrader().getCity()=="Milan"))
System.out.println("存在交易员在Milan");
else
System.out.println("不存在交易员在Milan");
}
/**
* 返回所有交易员的姓名字符串,按字母顺序排序。
*/
private static void practice04(){
transactions.stream()
.map(transaction->transaction.getTrader().getName())
.distinct()
.sorted()
.forEach(System.out::println);
}
/**
* 找所有来自于剑桥的交易员,并按姓名排序
*/
private static void practice03(){
transactions.stream()
.filter(transacion->transacion.getTrader().getCity()=="Cambridge")
.map(transaction->transaction.getTrader())
.distinct()
.sorted((o1,o2)->o1.getName().compareTo(o2.getName()))
.forEach(System.out::println);
}
/**
* 交易员都在哪些不同的城市工作过?
*/
private static void practice02(){
transactions.stream()
.map(stransaction-> stransaction.getTrader().getCity())
.distinct()
.forEach(System.out::println);
}
/**
* 找出2011年发生的所有交易,并按交易额排序(从低到高)
*/
private static void practice01(){
transactions.stream()
.filter(transaction -> transaction.getYear()==2011)
.sorted(((o1, o2) -> o1.getValue()-o2.getValue()))
.forEach(transaction -> System.out.println(transaction.toString()));
}
}
输出:
Transaction{trader=Trader{name='Brian', city='Cambridge'}, year=2011, value=300}
Transaction{trader=Trader{name='Raoul', city='Cambridge'}, year=2011, value=400}
-------------------------------
Cambridge
Milan
-------------------------------
Trader{name='Alan', city='Cambridge'}
Trader{name='Brian', city='Cambridge'}
Trader{name='Raoul', city='Cambridge'}
-------------------------------
Alan
Brian
Mario
Raoul
-------------------------------
存在交易员在Milan
-------------------------------
Canbridge的所有交易额 = 2650
-------------------------------
1000
-------------------------------
300
-------------------------------
进程已结束,退出代码0
总结
该练习难度不大,我们可以通过它来巩固学的Stream流方法;这里主要用到 sorted,match,map,filter等方法。如果想要具体学其他的Stream 流方法,可以进入我主页看我的 JDK8 新特性。