1.交易员pojo
/**
* @Author: donghai
* @Description:
* @Date: created on 2019/12/20 16:01
*/
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;
}
@Override
public String toString(){
return "Trader:"+this.name + " in " + this.city;
}
}
2.交易中心
/**
* @Author: donghai
* @Description:
* @Date: created on 2019/12/20 16:02
*/
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;
}
@Override
public String toString(){
return "{" + this.trader + ", " +
"year: "+this.year+", " +
"value:" + this.value +"}";
}
}
3.测试主类。所有注释都在这个类中,看代码即可!
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
/**
* @Author: donghai
* @Description:
* @Date: created on 2019/12/20 16:02
*/
public class MainTrader {
/**
* 交易员和交易
*
* @param args
*/
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 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)
);
//1.找出2011年发生的所有交易,并按交易额排序(从低到高)。
List<Transaction> collect = transactions.stream()
.filter(a -> a.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(Collectors.toList());
collect.forEach(a-> System.out.println(a.getYear()+":"+a.getValue()));
//(2) 交易员都在哪些不同的城市工作过?
Stream<String> distinct = transactions.stream().map(a -> a.getTrader().getCity()).distinct();
distinct.forEach(a-> System.out.println(a));
//(3) 查找所有来自于剑桥的交易员,并按姓名排序。
List<Trader> cambridge = transactions.stream()
.filter(a -> a.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getTrader)
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.collect(Collectors.toList());
cambridge.forEach(a-> System.out.println(a.getName()));
//(4) 返回所有交易员的姓名字符串,按字母顺序排序。---其内部会用到StringBuilder
String reduce = transactions.stream()
.map(a -> a.getTrader().getName())
.distinct()
.sorted()
.collect(joining());
System.out.println(reduce);
// collect.forEach(a-> System.out.println(a.getName()));
//(5) 有没有交易员是在米兰工作的?
boolean milan = transactions.stream()
.anyMatch(a -> a.getTrader().getCity().equals("Milan"));
System.out.println(milan);
//(6) 打印生活在剑桥的交易员的所有交易
transactions.stream()
.filter(a -> a.getTrader().getCity().equals("Cambridge"))
.forEach(a-> System.out.println(a.getValue()));
//(7) 所有交易中,最高的交易额是多少
System.out.println( transactions.stream()
.map(Transaction::getValue)
.reduce(Integer::max));
//(8) 找到交易额最小的交易
System.out.println( transactions.stream()
.map(Transaction::getValue)
.reduce(Integer::min));
//去重
List<Long> userIdList = new ArrayList<>();
userIdList.add(222L);
userIdList.add(222L);
userIdList.add(33L);
userIdList.add(43L);
List<Long> userIdFinalList = userIdList.stream().distinct().collect(Collectors.toList());
userIdFinalList.forEach(aLong -> System.out.println(aLong));
// removeif 删除 符合条件的 数据
List<Integer> longList = new ArrayList<>();
longList.add(1);
longList.add(5);
longList.add(9);
longList.add(19);
longList.add(30);
longList.removeIf(a ->
a > 5 && a < 30
);
longList.forEach(a ->
System.out.println(a));
);
}
}