APIExecrise.java
package jdk8;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class APIExecrise {
List<Employee> emps = Arrays.asList(
new Employee(101, "李四", 59, 6666.66, "busy"),
new Employee(102, "张三", 18, 9999.99, "free"),
new Employee(103, "王五", 28, 3333.33, "vocation"),
new Employee(104, "赵六", 8, 7777.77, "free"),
new Employee(105, "田七", 38, 5555.55, "busy")
);
List<Transaction> transactions = Arrays.asList(
new Transaction(new Trade("Raoul", "Cambridge"), 2011, 300),
new Transaction(new Trade("Mario", "Milan"), 2011, 400),
new Transaction(new Trade("Alan", "Cambridge"), 2012, 710),
new Transaction(new Trade("Brian", "Cambridge"), 2012, 950)
);;
/**
* 1.给定一个数字列表,返回一个数字平方
*/
@Test
public void test01() {
Integer[] nums = new Integer[]{1,2,3,4,5};
Arrays.stream(nums)
.map(n -> n * n)
.forEach(System.out :: println);
}
/**
* 2.用map和reduce数一数流中有多少个Employee
*/
@Test
public void test02() {
emps.stream()
.map(e -> 1)
.reduce(Integer::sum)
.ifPresent(System.out::println);
}
/**
* 1.找出2011年发生的所有交易,按交易额从低到高排序
*/
@Test
public void test1() {
transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
.forEach(System.out::println);
}
/**
* 2.交易员都在哪些不同的城市工作过
*/
@Test
public void test2() {
transactions.stream()
.map(t -> t.getTrade().getCity())
.forEach(System.out::println);
}
/**
* 3.查找来自剑桥的交易员,按姓名排序
*/
@Test
public void test3() {
transactions.stream()
.filter(t -> "Cambridge".equals(t.getTrade().getCity()))
.map(Transaction::getTrade)
.sorted(Comparator.comparing(Trade::getName))
.forEach(System.out::println);
}
/**
* 4.返回所有交易员姓名字符串,按字母顺序排序
*/
@Test
public void test4() {
transactions.stream()
.map(t -> t.getTrade().getName())
.sorted()
.forEach(System.out::println);
}
/**
* 5.有没有交易员在米兰工作过
*/
@Test
public void test5() {
boolean b = transactions.stream()
// .map(Transaction::getTrade)
.anyMatch(t -> "Milan".equals(t.getTrade().getCity()));
System.out.println(b);
}
/**
* 6.打印在剑桥所有的交易额
*/
@Test
public void test6() {
transactions.stream()
.filter(t -> "Cambridge".equals(t.getTrade().getCity()))
.map(Transaction::getValue)
.reduce(Integer::sum)
.ifPresent(System.out::println);
}
/**
* 7.查出最高的交易额
*/
@Test
public void test7() {
transactions.stream()
.map(Transaction::getValue)
.max(Integer::compareTo)
.ifPresent(System.out::println);
}
/**
* 8.找到交易额最小的交易
*/
@Test
public void test8() {
transactions.stream()
.min(Comparator.comparingInt(Transaction::getValue))
.ifPresent(System.out::println);
}
}
class Transaction {
List<Transaction> transactions = null;
private Trade trade;
private int year;
private int value;
@Before
public void before() {
Trade raoul = new Trade("Raoul", "Cambridge");
Trade mario = new Trade("Mario", "Milan");
Trade alan = new Trade("Alan", "Cambridge");
Trade brian = new Trade("Brian", "Cambridge");
transactions = Arrays.asList(
new Transaction(brian, 2011, 300),
new Transaction(raoul, 2011, 400),
new Transaction(mario, 2012, 710),
new Transaction(alan, 2012, 950)
);
}
public Transaction(Trade trade, int year, int value) {
this.trade = trade;
this.year = year;
this.value = value;
}
public Transaction() {
}
public Trade getTrade() {
return trade;
}
public void setTrade(Trade trade) {
this.trade = trade;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "Transaction{" +
"trade=" + trade +
", year=" + year +
", value=" + value +
'}';
}
}
class Trade {
private String name;
private String 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;
}
public Trade(String name, String city) {
this.name = name;
this.city = city;
}
public Trade() {
}
@Override
public String toString() {
return "Trade{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
'}';
}
}
class Employee {
private int id;
private String name;
private int age;
private double salary;
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
Employee(int id, String name, int age, double salary, String status) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
}
public Employee() {
}
}