zoukankan      html  css  js  c++  java
  • java8中的Stream

    Collection.stream() / parallelStream()

    1. Stream

    1)Filter

       stringCollection .stream().filter((s) -> s.startsWith("a")) .forEach(System.out::println); 

    2)Sorted

      stringCollection .stream().sorted()

    3)Map

      stringCollection .stream().map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a)) 

    4)Matches

    boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));

    boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));

    boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));

    5)Count 

    long startsWithB = stringCollection.stream().filter((s) -> s.startsWith("b")) .count(); 
    6)Reduce
    stringCollection .stream().sorted().reduce((s1, s2) -> s1 + "#" + s2); 
     
    2. Parallel stream
    1) sorted
        long t0 = System.nanoTime();

        long count = values.parallelStream().sorted().count(); System.out.println(count);

        long t1 = System.nanoTime();

        long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms 

    2) MAP

    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i);}

    map.forEach((id, val) -> System.out.println(val)); 

    map.computeIfPresent(3, (num, val) -> val + num); map.get(3); // val33

    map.computeIfPresent(9, (num, val) -> null); map.containsKey(9); // false

    map.computeIfAbsent(23, num -> "val" + num); map.containsKey(23); // true

    map.computeIfAbsent(3, num -> "bam"); map.get(3); // val33 

    map.getOrDefault(42, "not found"); // not found 

    map.merge(9, "val9", (value, newValue) -> value.concat(newValue)); map.get(9); // val9
    map.merge(9, "concat", (value, newValue) -> value.concat(newValue)); map.get(9); // val9concat 
  • 相关阅读:
    mysql数据库表中判断字段是否存在,如果不存在则创建该字段
    PHP同时操作两个mysql数据库
    Bootstrap-分页插件Paginator
    NLP--自然语言处理与机器学习会议
    对CURL的一些研究
    Nginx完整配置说明
    SecureCRT自动登陆到服务器的脚本以及脚本编写简单说明
    Fast CGI 工作原理
    FastCGI中文规范
    Linux +apache+fastcgi运行c/c++
  • 原文地址:https://www.cnblogs.com/feika/p/4576794.html
Copyright © 2011-2022 走看看