/** * StreamAPI的小练习 */public class StreamAPIExercise { private Listtransactions; @Before public void before() { Trader yan = new Trader("Yan", "Cambridge"); Trader zh = new Trader("ZH", "Milan"); Trader rose = new Trader("Rose", "Cambridge"); Trader lee = new Trader("Lee", "Cambridge"); transactions = Arrays.asList(new Transaction(yan, 2011, 300), new Transaction(zh, 2012, 1000), new Transaction(zh, 2011, 400), new Transaction(rose, 2012, 710), new Transaction(rose, 2012, 700), new Transaction(lee, 2012, 950) ); } //给定一组数字的列表,返回由每个数字平方构成的列表 @Test public void test1() { Integer[] nums = new Integer[]{1, 2, 3, 4, 5}; Arrays.stream(nums).map((e) -> e * e).forEach(System.out::println); } //使用map reduce 方法数出流中有多少个Employee @Test public void test2() { Optional sum = employees.stream().map(e -> 1).reduce(Integer::sum); System.out.println(sum.get()); } List employees = Arrays.asList( new Employee("Yan", 18, '男', 0), new Employee("Mike", 20, '女', 5000), new Employee("Lee", 18, '女', 8000), new Employee("Rose", 22, '男', 9000), new Employee("Sk", 30, '男', 11000) ); //找出2011年所有交易 并按照交易额排序(低->高) @Test public void test3() { transactions.stream().filter((e) -> e.getYear() == 2011) //.sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue())) .sorted(new Comparator () { @Override public int compare(Transaction o1, Transaction o2) { return Integer.compare(o1.getValue(), o2.getValue()); } }) .forEach(System.out::println); } //交易员都在哪些不同城市工作过 @Test public void test4() { transactions.stream().map(t -> t.getTrader().getCity()).distinct().forEach(System.out::println); } //查找出来自剑桥的交易员,并按姓名排序 @Test public void test5() { transactions.stream().filter(t -> t.getTrader().getCity() == "Cambridge") .map(t -> t.getTrader().getName())// .sorted((t1, t2) -> t1.compareTo(t2)) .distinct() .sorted(new Comparator () { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }) .forEach(System.out::println); } //返回所有交易员姓名的字符串,并按字母顺序排序 @Test public void test6() { transactions.stream().map(t->t.getTrader().getName()) .sorted().forEach(System.out::println); System.out.println("------------------------------------------------"); //姓名连成一个字符串 System.out.println(transactions.stream().map(t -> t.getTrader().getName()) .sorted().reduce("", String::concat)); System.out.println("------------------------------------------------"); //姓名连成一个字符串 并按照字母排序 并且忽略大小写 //思路:1.取出集合中每个姓名 2.对每个姓名 取出所有字符 存入list集合 3.list.stream 将stream sorted排序 transactions.stream().map(t -> t.getTrader().getName()) .flatMap(StreamAPIExercise::filterCharacter) .sorted((o1, o2) -> o1.compareToIgnoreCase(o2)) .forEach(System.out::print); } public static Stream filterCharacter(String str) { List list = new ArrayList<>(); for (Character ch : str.toCharArray()) { list.add(ch.toString()); } return list.stream(); } //有没有交易员在米兰工作 @Test public void test7() { boolean bl = transactions.stream().anyMatch(t -> t.getTrader().getCity() == "Milan"); } //打印剑桥的交易员所有交易额 @Test public void test8() { Optional sum = transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge")) .map(t -> t.getValue()) .reduce((x,y)->Integer.sum(x,y)); // .reduce(Integer::sum); System.out.println(sum); } //所有交易中最高的交易额是多少 @Test public void test9() { Optional max = transactions.stream().map(t -> t.getValue()).max((x,y)->Integer.compare(x,y)); System.out.println(max.get()); } //找出最小交易 @Test public void test10() { Optional min = transactions.stream().min((x, y) -> Integer.compare(x.getValue(), y.getValue())); System.out.println(min.get()); }}