Stream中间操作小练习
给定一个数字列表,如何返回一个由每个数的平方构成的列表呢
@Test
public void test1() {
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
nums.stream().map(x -> x * x).collect(Collectors.toList()).forEach(System.out::println);
}
map与reduce结合计算Employee中的个数
实体类
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee {
private String name;
private Integer age;
private Double salary;
private Status status;
public Employee(Integer age){
this.age = age;
}
public Employee(Integer age, Double salary){
this.age = age;
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(name, employee.name) &&
Objects.equals(age, employee.age) &&
Objects.equals(salary, employee.salary) &&
status == employee.status;
}
@Override
public int hashCode() {
return Objects.hash(name, age, salary, status);
}
public enum Status{
FREE,
BUSY,
VOCATION
}
}
@Test
public void test2(){
List<Employee> employees = Arrays.asList(
new Employee("张三", 18, 9999.99, Employee.Status.FREE),
new Employee("李四", 48, 3333.99,Employee.Status.BUSY),
new Employee("王五", 38, 5999.99, Employee.Status.VOCATION),
new Employee("赵六", 38, 5999.99, Employee.Status.BUSY),
new Employee("田七", 8, 6999.99, Employee.Status.VOCATION)
);
Optional<Integer> reduce = employees.stream().map((e) -> 1).reduce(Integer::sum);
System.out.println(reduce.get());
}
根据如下条件找出符合条件的记录数
1.找出2011年发生的所有交易,并按交易额排序(从低到高)
2.交易员都在哪些不同的城市工作过
3.查找所有来自剑桥的交易员,并按照姓名排序
4.返回所有交易员的姓名字符串,按照字母顺序排序
5.有没有交易员是在米兰工作的
6.打印生活在剑桥的交易员的所有交易额
7.所有交易中,最高的交易额是多少
8.找到交易额最小的交易
实体类
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Trader {
private String name;
private String city;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Transaction {
private Trader trader;
private int year;
private int value;
}
测试类
public class TestTransaction {
List<Transaction> transactions = null;
@Before
public void before() {
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)
);
}
@Test
public void test1() {
transactions.stream()
.filter(t->t.getYear()==2011)
.sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
.collect(Collectors.toList())
.forEach(System.out::println);
}
@Test
public void test2(){
transactions.stream()
.map(Transaction::getTrader)
.map(Trader::getCity)
.distinct()
.forEach(System.out::println);
transactions.stream()
.map(t->t.getTrader().getCity())
.distinct()
.forEach(System.out::println);
}
@Test
public void test3(){
transactions.stream()
.map(Transaction::getTrader)
.filter(x->x.getCity().equals("Cambridge"))
.sorted((x,y)->x.getName().compareTo(y.getName()))
.distinct()
.forEach(System.out::println);
System.out.println("----------------------------------------------");
transactions.stream()
.filter(x->x.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getTrader)
.sorted((x,y)->x.getName().compareTo(y.getName()))
.distinct()
.forEach(System.out::println);
}
@Test
public void test4(){
transactions.stream()
.map(Transaction::getTrader)
.map(Trader::getName)
.sorted()
.forEach(System.out::println);
System.out.println("--------------------------------------");
transactions.stream()
.map((t)->t.getTrader().getName())
.sorted()
.forEach(System.out::println);
System.out.println("--------------------------------------");
String reduce = transactions.stream()
.map((t) -> t.getTrader().getName())
.sorted()
.reduce("", String::concat);
System.out.println(reduce);
System.out.println("--------------------------------------");
transactions.stream()
.map((t)->t.getTrader().getName())
.flatMap(TestTransaction::filterCharacter)
.sorted((s1,s2)->s1.compareToIgnoreCase(s2))
.forEach(System.out::print);
}
public static Stream<String> filterCharacter(String str){
List<String> list = new ArrayList<>();
for(Character ch:str.toCharArray()){
list.add(ch.toString());
}
return list.stream();
}
@Test
public void test5(){
boolean milan = transactions.stream()
.map(Transaction::getTrader)
.anyMatch(x -> x.getCity().equals("Milan"));
System.out.println(milan);
System.out.println("------------------------------------------");
boolean b = transactions.stream()
.anyMatch(x -> x.getTrader().getCity().equals("Milan"));
System.out.println(b);
}
@Test
public void test6(){
Optional<Integer> sum = transactions.stream()
.filter((x) -> x.getTrader().getCity().equals("Cambridge"))
.map(Transaction::getValue)
.reduce(Integer::sum);
System.out.println(sum.get());
}
@Test
public void test7(){
DoubleSummaryStatistics collect = transactions.stream()
.collect(Collectors.summarizingDouble(Transaction::getValue));
System.out.println(collect.getMax());
System.out.println("--------------------------------");
Optional<Integer> max = transactions.stream()
.map((t) -> t.getValue())
.max(Integer::compare);
System.out.println(max.get());
}
@Test
public void test8(){
Optional<Transaction> collect = transactions.stream()
.collect(Collectors.minBy((x, y) -> Integer.compare(x.getValue(), y.getValue())));
System.out.println(collect.get());
System.out.println("--------------------------------");
Optional<Transaction> min = transactions.stream()
.min((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()));
System.out.println(min.get());
}
}