• java8 Stream API 的操作2 中间操作


    public class TestStreamAPI1 {
     
     List<Employee> emps = Arrays.asList(
       new Employee(102, "李四", 59, 6666.66),
       new Employee(101, "张三", 18, 9999.99),
       new Employee(103, "王五", 28, 3333.33),
       new Employee(104, "赵六", 8, 7777.77),
       new Employee(104, "赵六", 8, 7777.77),
       new Employee(104, "赵六", 8, 7777.77),
       new Employee(105, "田七", 38, 5555.55)
     );
     
     //2. 中间操作
     /*
      映射
      map——接收 Lambda , 将元素转换成其他形式或提取信息。接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
      flatMap——接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
      */
     @Test
     public void test1(){
      Stream<String> str = emps.stream()
       .map((e) -> e.getName());
      
      System.out.println("-------------------------------------------");
      
      List<String> strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
      
      Stream<String> stream = strList.stream()
          .map(String::toUpperCase);
      
      stream.forEach(System.out::println);
      
      Stream<Stream<Character>> stream2 = strList.stream()
          .map(TestStreamAPI1::filterCharacter);
      
      stream2.forEach((sm) -> {
       sm.forEach(System.out::println);
      });
      
      System.out.println("---------------------------------------------");
      
      Stream<Character> stream3 = strList.stream()
          .flatMap(TestStreamAPI1::filterCharacter);
      
      stream3.forEach(System.out::println);
     }

     public static Stream<Character> filterCharacter(String str){
      List<Character> list = new ArrayList<>();
      
      for (Character ch : str.toCharArray()) {
       list.add(ch);
      }
      
      return list.stream();
     }
     
     /*
      sorted()——自然排序
      sorted(Comparator com)——定制排序
      */
     @Test
     public void test2(){
      emps.stream()
       .map(Employee::getName)
       .sorted()
       .forEach(System.out::println);
      
      System.out.println("------------------------------------");
      
      emps.stream()
       .sorted((x, y) -> {
        if(x.getAge() == y.getAge()){
         return x.getName().compareTo(y.getName());
        }else{
         return Integer.compare(x.getAge(), y.getAge());
        }
       }).forEach(System.out::println);
     }

  • 相关阅读:
    [bzoj3224] 普通平衡树
    [总结] 三种常见的区间贪心问题
    [NOIP2014] 飞扬的小鸟
    POJ 1185 炮兵阵地
    WOJ 1538 Stones II 转化背包问题
    WOJ 1542 Countries 并查集转化新点+最短路
    UVA 11375 高精度Bign类
    2014_csu选拔1_B
    Codeforces 405D 数学问题
    Codeforces 400C 矩阵乘法 数学规律
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13300656.html
Copyright © 2020-2023  润新知