package com.wuzhixin.practice;
import com.wuzhixin.javaendapi.Employee2;
import org.junit.Before;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
public class TestPractice {
List<Employee2> employee2s = Arrays.asList(
new Employee2(22, "WUZHIXIN", 44344.3, Employee2.EmployeeStatus.Free),
new Employee2(11, "x", 22.2, Employee2.EmployeeStatus.Busy),
new Employee2(22, "S", 33.23, Employee2.EmployeeStatus.Vocation),
new Employee2(33, "SS", 33.22, Employee2.EmployeeStatus.Free),
new Employee2(33, "SS", 33.22, Employee2.EmployeeStatus.Free));
/**
* 给定一个数字列表,如何返回一个
* 由每个数的平方构成的列表呢?
*
* 给定 1,2,3,4,5
*
* 返回 1,4,9,16,25
*/
@Test
public void test(){
Integer integer[] = new Integer[]{1,2,3,4,5};
Stream<Integer> stream = Arrays.stream(integer);
stream.map(x->x*x).forEach(System.out::println);
}
/**
* 怎样用 map 和 reduce 方法数多少个employee
*/
@Test
public void test2(){
long count = employee2s.stream().map(e->1).count();
Optional<Integer> reduce = employee2s.stream().map(e -> 1).reduce(Integer::sum);
System.out.println(count);
System.out.println(reduce.get());
}
/**
* 1.找出2011年发生的所有交易,并按交易额排序(从高到低)
* 2.交易员都在哪些不同的城市工作过
* 3.查找所有来自北京的交易员,按姓名排序
* 4。返回所有交易员的姓名字符串,按字母顺序排序
* 5有没有交易员在上海工作过
* 6打印生活在北京的交易员的所有交易额
* 7所有交易中,最高的交易额是多少
* 8找到交易额最小的交易
*
*/
@Test
public void test3(){
List<Transaction> transactions= null;
/**
* 1 1.找出2011年发生的所有交易,并按交易额排序(从高到低)
*/
Trader a = new Trader("a","beijing");
Trader b = new Trader("b","shanghai");
Trader c = new Trader("c","guangzhou");
Trader d = new Trader("d","shenzhen");
transactions = Arrays.asList(
new Transaction(d,2011,300),
new Transaction(a,2012,1000),
new Transaction(a,2011,400),
new Transaction(b,2012,710),
new Transaction(b,2012,700),
new Transaction(c,2012,950)
);
transactions.stream()
.filter(t->t.getYear() == 2011)
.sorted((x,y)->Integer.compare(x.getValue(),y.getValue()))
.forEach(System.out::println);
/**
* .交易员都在哪些不同的城市工作过
*/
transactions.stream().map(t->t.getTrader().getCity()).distinct().
forEach(System.out::println);
/**
* 3.查找所有来自北京的交易员,按姓名排序
*/
transactions.stream()
.filter(t -> t.getTrader().getCity().equals("beijing"))
.map(Transaction::getTrader)
.sorted((x,y)->x.getName().compareTo(y.getName()))
.forEach(System.out::println);
/**
* 4。返回所有交易员的姓名字符串,按字母顺序排序
*/
System.out.println("=============");
transactions.stream().map(t->t.getTrader().getName()).sorted((x,y)->x.compareTo(y)).distinct()
.forEach(System.out::println);
/**
* 5有没有交易员在上海工作过
*/
boolean shanghai = transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("shanghai"));
System.out.println(shanghai);
/**
* 6.打印生活在北京的交易员的所有交易额
*/
Optional<Integer> beijing = transactions.stream().filter(t -> t.getTrader().getCity().equals("beijing"))
.map(t -> t.getValue()).reduce(Integer::sum);
System.out.println(beijing.get());
/**
* 7所有交易中,最高的交易额是多少
*/
System.out.println("所有交易中,最高的交易额是多少");
Optional<Integer> max = transactions.stream().map(t -> t.getValue()).max(Integer::compare);
System.out.println(max.get());
/**
* 8找到交易额最小的交易
*/
System.out.println("8找到交易额最小的交易");
Optional<Transaction> min = transactions.stream().min((x, y) -> Integer.compare(x.getValue(), y.getValue()));
System.out.println(min.get());
}
}
Java8 stream api 操练
最新推荐文章于 2023-11-02 12:09:21 发布