草稿 Stream

概念

  • 顺序遍历每一个进行操作 和 对集合的聚合操作 可以类比数据库的聚合函数 sequential and parallel aggregate operations

顶层接口

  • BaseStream

    • 终端操作
      • Iterator<T> iterator() 返回集合的迭代器
      • Spliterator<T> spliterator() 返回集合的分割器 没搞懂怎么用,干啥用的?
    • 中间状态操作

      • S sequential() 返回一个顺序流

                int[] array = {1,2,4,6,3,1,3};
                Stream<int[]> sequential = Stream.of(array).sequential();
      • S parallel() 返回一个并行流??
      • S unordered() 返回一个无序流 ??
    • 其他操作

      • boolean isParallel() 判断当前流是否可执行终端操作
      • S onClose(Runnable closeHandler) 当流被关闭时触发的动作。
  • Collector

    • Supplier<A> supplier() 返回一个可变的容器??
    • BiConsumer<A,T> accumulator() 把多个值折叠入一个可变容器???
    • BinaryOperator<A> combiner() 组合两个结果
    • Function<A,R> finisher() 把一个中间结果变成一个最终结果
    • Set<Collector.Characteristics> characteristics() 特性集合???
    • static <T,R> Collector<T,R,R> of(Supplier<R> supplier,
      BiConsumer<R,T> accumulator,
      BinaryOperator<R> combiner,
      Collector.Characteristics... characteristics)
      Collector的构造器
         Collector<Widget, ?, TreeSet<Widget>> intoSet =
             Collector.of(TreeSet::new, TreeSet::add,
                          (left, right) -> { left.addAll(right); return left; });
         Collector<Employee, ?, Integer> summingSalaries
             = Collectors.summingInt(Employee::getSalary))
    
     Collector<Employee, ?, Map<Department, Integer>> summingSalariesByDept
         = Collectors.groupingBy(Employee::getDepartment, summingSalaries);
  • DoubleStream double相关的所有的你能想到的操作

    • DoubleStream peek(DoubleConsumer action)
           DoubleStream.of(1, 2, 3, 4)
               .filter(e -> e > 2)
               .peek(e -> System.out.println("Filtered value: " + e))
               .map(e -> e * e)
               .peek(e -> System.out.println("Mapped value: " + e))
               .sum();
    
         double sum = widgets.stream()
                             .filter(w -> w.getColor() == RED)
                             .mapToDouble(w -> w.getWeight())
                             .sum();
  • Collectors

     // Accumulate names into a List
         List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
    
         // Accumulate names into a TreeSet
         Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
    
         // Convert elements to strings and concatenate them, separated by commas
         String joined = things.stream()
                               .map(Object::toString)
                               .collect(Collectors.joining(", "));
    
         // Compute sum of salaries of employee
         int total = employees.stream()
                              .collect(Collectors.summingInt(Employee::getSalary)));
    
         // Group employees by department
         Map<Department, List<Employee>> byDept
             = employees.stream()
                        .collect(Collectors.groupingBy(Employee::getDepartment));
    
         // Compute sum of salaries by department
         Map<Department, Integer> totalByDept
             = employees.stream()
                        .collect(Collectors.groupingBy(Employee::getDepartment,
                                                       Collectors.summingInt(Employee::getSalary)));
    
         // Partition students into passing and failing
         Map<Boolean, List<Student>> passingFailing =
             students.stream()
                     .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));

用例

 - sum 求和
     int sum = widgets.stream()
                      .filter(w -> w.getColor() == RED)
                      .mapToInt(w -> w.getWeight())
                      .sum();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis是一种内存数据库,可以用于实现草稿箱功能。下面是一个使用Redis实现草稿箱的简单示例: 1. 连接到Redis:首先,你需要通过Java Redis客户端连接到Redis数据库。例如,可以使用Jedis或Lettuce等流行的Redis客户端库。 2. 保存草稿:当用户选择保存内容为草稿时,将草稿的内容存储在Redis中。可以使用哈希表(Hash)来表示每个草稿,其中键是草稿的唯一标识符,而字段和值可以表示草稿的各个属性,如标题、内容和创建时间等。 ```java Jedis jedis = new Jedis("localhost", 6379); String draftId = "draft:123"; Map<String, String> draftData = new HashMap<>(); draftData.put("title", "My Draft"); draftData.put("content", "This is my draft content"); draftData.put("created", "2021-10-01"); jedis.hmset(draftId, draftData); ``` 3. 获取草稿:当用户需要编辑草稿时,通过草稿的唯一标识符从Redis中获取草稿的详细信息。 ```java Map<String, String> draftData = jedis.hgetAll(draftId); String title = draftData.get("title"); String content = draftData.get("content"); // 显示在编辑界面供用户修改 ``` 4. 更新草稿:当用户对草稿进行修改后,更新Redis中对应草稿的内容。 ```java Map<String, String> updatedData = new HashMap<>(); updatedData.put("title", "Updated Draft"); updatedData.put("content", "This is the updated draft content"); jedis.hmset(draftId, updatedData); ``` 5. 删除草稿:如果用户决定删除草稿,从Redis中删除对应的草稿数据。 ```java jedis.del(draftId); ``` 需要注意的是,上述示例只提供了基本的操作,实际应用中可能还需要考虑并发访问、草稿列表的管理、过期时间设置等其他方面的功能。此外,你还可以根据具体需求添加其他字段或操作来扩展草稿功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值