一、Lambda 表达式
Lambda 表达式的初衷是,进一步简化匿名类的语法(不过实现上,Lambda 表达式并不是匿名类的语法糖)
1、使用 Stream 简化集合操作;
map 方法传入的是一个 Function,可以实现对象转换;
filter 方法传入一个 Predicate,实现对象的布尔判断,只保留返回 true 的数据;
mapToDouble 用于把对象转换为 double;
通过 average 方法返回一个 OptionalDouble,代表可能包含值也可能不包含值的可空 double。
2、使用 Optional 简化判空逻辑;
Optional.ofNullable(responseDTO.getHelpFlag()).orElse(false); 

3、JDK8 结合 Lambda 和 Stream 对各种类的增强。
场景:要通过 HashMap 实现一个缓存的操作,在 Java 8 之前我们可能会写出这样的 getProductAndCache 方法:先判断缓存中是否有值;如果没有值,就从数据库搜索取值;最后,把数据加入缓存。
private Map<Long, Product> cache = new ConcurrentHashMap<>();
private Product getProductAndCache(Long id) {
    Product product = null;
    //Key存在,返回Value
    if (cache.containsKey(id)) {
        product = cache.get(id);
    } else {
        //不存在,则获取Value
        //需要遍历数据源查询获得Product
        for (Product p : Product.getData()) {
            if (p.getId().equals(id)) {
                product = p;
                break;
            }
        }
        //加入ConcurrentHashMap
        if (product != null)
            cache.put(id, product);
    }
    return product;
}
@Test
public void notcoolCache() {
    getProductAndCache(1L);
    getProductAndCache(100L);
    System.out.println(cache);
    assertThat(cache.size(), is(1));
    assertTrue(cache.containsKey(1L));
} 
Java 8 中,我们利用 ConcurrentHashMap 的 computeIfAbsent 方法,用一行代码就可以实现这样的繁琐操作:
private Product getProductAndCacheCool(Long id) {
    return cache.computeIfAbsent(id, i -> //当Key不存在的时候提供一个Function来代表根据Key获取Value的过程
            Product.getData().stream()
                    .filter(p -> p.getId().equals(i)) //过滤
                    .findFirst() //找第一个,得到Optional<Product>
                    .orElse(null)); //如果找不到Product,则使用null
}
@Test
public void coolCache()
{
    getProductAndCacheCool(1L);
    getProductAndCacheCool(100L);
    System.out.println(cache);
    assertThat(cache.size(), is(1));
    assertTrue(cache.containsKey(1L));
} 
computeIfAbsent 方法在逻辑上相当于:
if (map.get(key) == null) {
  V newValue = mappingFunction.apply(key);
  if (newValue != null)
    map.put(key, newValue);
} 
4、并行流
此外 Java 8 还提供了并行流的功能:通过 parallel 方法,一键把 Stream 转换为并行操作提
如下代码通过线程池来并行消费处理 1 到 100:
IntStream.rangeClosed(1,100).parallel().forEach(i->{
    System.out.println(LocalDateTime.now() + " : " + i);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) { }
}); 
5、并行方案
使用 CompletableFuture 来实现。CompletableFuture.runAsync 方法可以指定一个线程池,一般会在使用 CompletableFuture 的时候用到:
private int completableFuture(int taskCount, int threadCount) throws InterruptedException, ExecutionException {
    //总操作次数计数器
    AtomicInteger atomicInteger = new AtomicInteger();
    //自定义一个并行度=threadCount的ForkJoinPool
    ForkJoinPool forkJoinPool = new ForkJoinPool(threadCount);
    //使用CompletableFuture.runAsync通过指定线程池异步执行任务
    CompletableFuture.runAsync(() -> IntStream.rangeClosed(1, taskCount).parallel().forEach(i -> increment(atomicInteger)), forkJoinPool).get();
    //查询计数器当前值
    return atomicInteger.get();
} 
 
 
 
 
 
 
 
 
 
 
 
                












![[Go版]算法通关村第十五关黄金——继续研究超大规模数据场景的问题](https://img-blog.csdnimg.cn/img_convert/642e2a0f511c3e4c451a8ca116d31346.png#pic_center)





