资讯专栏INFORMATION COLUMN

Lambda表达式与Stream流 (终)

lidashuang / 2598人阅读

摘要:陈杨一表达式与流二初始化测试数据三各种方法一方法方法二方法

package com.java.design.java8;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author  陈杨
 * 
 */
 
@SpringBootTest
@RunWith(SpringRunner.class)
public class LambdaInfo {
一、Lambda表达式与Stream流
/*
    A lambda expression can be understood as a concise representation of an anonymous function
        that can be passed around: it doesn’t have a name,
        but it has a list of parameters, a body, a return type,
        and also possibly a list of exceptions that can be thrown.
            That’s one big definition;

    let’s break it down:
    Anonymous:
        We say anonymous because it doesn’t have an explicit name like a method would normally have: less to write and think about!
    Function:
        We say function because a lambda isn’t associated with a particular class like a method is.
        But like a method, a lambda has a list of parameters, a body, a return type,
        and a possible list of exceptions that can be thrown.
    Passed around:
        A lambda expression can be passed as argument to a method or stored in a variable.
    Concise:
        You don’t need to write a lot of boilerplate like you do for anonymous classes.

*/

/*
    Stream :  A sequence of elements from a source that supports data processing operations.
        Sequence of elements
        Source
        Pipelining
        Internal iteration
        Traversable only once
        Collections: external interation using an interator behind the scenes

*/
二、初始化测试数据
private List list;

@Before
public void init() {

    list = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toList());

    list.sort(Collections.reverseOrder());
}
三、各种API

1.allMatch

@Test

public void testLambdaInfo() {


    System.out.println(">---------------------Match方法----------------------<");
       //  一、Match方法
        //  Returns whether all elements of this stream match the provided predicate.
        Optional.of(list.stream().mapToInt(Integer::intValue).allMatch(i -> i > 0))
                .ifPresent(System.out::println);

        //  Returns whether any elements of this stream match the provided predicate.
        Optional.of(list.stream().mapToInt(Integer::intValue).anyMatch(i -> i > 0))
                .ifPresent(System.out::println);

        //  Returns whether no elements of this stream match the provided predicate..
        Optional.of(list.stream().mapToInt(Integer::intValue).noneMatch(i -> i > 0))
                .ifPresent(System.out::println);

2、find

System.out.println(">--------------------Find方法-----------------------<");

//  二、Find方法
//  Returns an  Optional describing the first element of this stream,
//  or an empty Optional if the stream is empty.
//  If the stream has no encounter order, then any element may be returned.
list.stream().mapToInt(Integer::intValue).filter(i -> i > 10).findFirst()
        .ifPresent(System.out::println);

//  Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
list.stream().mapToInt(Integer::intValue).filter(i -> i > 10).findAny()
        .ifPresent(System.out::println);

3、reduce

System.out.println(">---------------------Reduce方法----------------------<");

//  三、Reduce方法
//  Performs a reduction on the elements of this stream, using the provided identity value
//      and an associative accumulation function, and returns the reduced value.

//  求和
System.out.println(list.stream().reduce(0, Integer::sum));
list.stream().mapToInt(Integer::intValue).reduce(Integer::sum)
        .ifPresent(System.out::println);


//  求最大值
System.out.println(list.stream().reduce(0, Integer::max));
list.stream().mapToInt(Integer::intValue).reduce(Integer::max)
        .ifPresent(System.out::println);


//  求最小值
System.out.println(list.stream().reduce(0, Integer::min));
list.stream().mapToInt(Integer::intValue).reduce(Integer::min)
        .ifPresent(System.out::println);
               System.out.println(">-------------------------------------------<");

    }

4、CompletableFuture API

@Test
public void testCompletableFuture() {

    //  四、CompletableFuture API
    /*
     * Returns a new CompletableFuture that is asynchronously completed by a task
     * running in the given executor with the value obtained by calling the given Supplier.
     */
    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum, System.out::println);


    Optional.of(CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum)
            .complete(55)).ifPresent(System.out::println);

    //  thenAccept 无返回值  Consumer action
    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum)
            .thenAccept(System.out::println);

    //  thenApply  有返回值  Function fn
    CompletableFuture.supplyAsync(() -> list.stream().mapToInt(Integer::intValue))
            .thenApply(IntStream::sum).thenAccept(System.out::println);

    //  对元素及异常进行处理  BiFunction fn
    CompletableFuture.supplyAsync(() -> list.stream().mapToInt(Integer::intValue))
            .handle((i, throwable) -> "handle:	" + i.sum()).thenAccept(System.out::println);

    //  whenCompleteAsync 完成时执行 BiConsumer action
    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum)
            .whenCompleteAsync((value, throwable) -> System.out.println("whenCompleteAsync:	" + value));

    //   组合CompletableFuture 将前一个结果作为后一个输入参数 (参照 组合设计模式)
    CompletableFuture.supplyAsync(() -> list.stream().mapToInt(Integer::intValue))
            .thenCompose(i -> CompletableFuture.supplyAsync(i::sum)).thenAccept(System.out::println);

    //   合并CompletableFuture
    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum)
            .thenCombine(CompletableFuture.supplyAsync(() -> list.stream()
                    .mapToDouble(Double::valueOf).sum()), Double::sum).thenAccept(System.out::println);

    //   合并CompletableFuture
    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue)::sum)
            .thenAcceptBoth(CompletableFuture.supplyAsync(list.stream()
                            .mapToDouble(Double::valueOf)::sum),
                    (r1, r2) -> System.out.println("thenAcceptBoth:	" + r1 + "	" + r2));

    //  2个CompletableFuture运行完毕后运行Runnable
    CompletableFuture.supplyAsync(() -> {
        System.out.println(Thread.currentThread().getName() + "	is running");
        return list.stream().mapToInt(Integer::intValue).sum();
    })
            .runAfterBoth(
                    CompletableFuture.supplyAsync(() -> {
                        System.out.println(Thread.currentThread().getName() + "	is running");
                        return list.stream().mapToDouble(Double::valueOf).sum();
                    }),
                    () -> System.out.println("The 2 method have done"));


    //  2个CompletableFuture 有一个运行完就执行Runnable
    CompletableFuture.supplyAsync(() -> {
        System.out.println(Thread.currentThread().getName() + "	is running");
        return list.stream().mapToInt(Integer::intValue).sum();
    })
            .runAfterEither(
                    CompletableFuture.supplyAsync(() -> {
                        System.out.println(Thread.currentThread().getName() + "	is running");
                        return list.stream().mapToDouble(Double::valueOf).sum();
                    }),
                    () -> System.out.println("The 2 method have done"));


    //  2个CompletableFuture 有一个运行完就执行Function fn
    CompletableFuture.supplyAsync(
            list.stream().mapToInt(Integer::intValue).max()::getAsInt)
            .applyToEither(
                    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue).min()::getAsInt)
                    , v -> v * 10)
            .thenAccept(System.out::println);

    //  2个CompletableFuture 有一个运行完就执行Consumer action
    CompletableFuture.supplyAsync(
            list.stream().mapToInt(Integer::intValue).max()::getAsInt)
            .acceptEither(
                    CompletableFuture.supplyAsync(list.stream().mapToInt(Integer::intValue).min()::getAsInt)
                    , System.out::println);

    //  将集合中每一个元素都映射成为CompletableFuture对象
    List> collect =
            list.stream().map(i -> CompletableFuture.supplyAsync(i::intValue))
                    .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    //  集合转数组
    CompletableFuture[] completableFutures = collect.toArray(CompletableFuture[]::new);

    //  有一个task执行完毕
    CompletableFuture.anyOf(completableFutures)
            .thenRun(() -> System.out.println("有一个task执行完毕--->first done"));
    //  有且仅有所有task执行完毕
    CompletableFuture.allOf(completableFutures)
            .thenRun(() -> System.out.println("有且仅有所有task执行完毕--->done"));

}

5、Java.time API

    @Test
    public void testLocalDateTime() {

        //  五、Java.time API
        LocalDate localDate = LocalDate.of(2019, 12, 1);

        //  当前时间
        Optional.of(LocalDate.now()).ifPresent(System.out::println);

        //  年份
        Optional.of(localDate.getYear()).ifPresent(System.out::println);
        OptionalInt.of(localDate.get(ChronoField.YEAR)).ifPresent(System.out::println);

        //  月份 (Jan-->Dec)
        Optional.of(localDate.getMonth()).ifPresent(System.out::println);


        //  月份(1-->12)
        Optional.of(localDate.getMonthValue()).ifPresent(System.out::println);
        OptionalInt.of(localDate.get(ChronoField.MONTH_OF_YEAR)).ifPresent(System.out::println);


        //  年中的第几天
        Optional.of(localDate.getDayOfYear()).ifPresent(System.out::println);
        OptionalInt.of(localDate.get(ChronoField.DAY_OF_YEAR)).ifPresent(System.out::println);


        //  月中的第几天
        Optional.of(localDate.getDayOfMonth()).ifPresent(System.out::println);
        OptionalInt.of(localDate.get(ChronoField.DAY_OF_MONTH)).ifPresent(System.out::println);

        //  星期几(Mon-->Sun)
        Optional.of(localDate.getDayOfWeek()).ifPresent(System.out::println);

        //  星期几(1-->7)
        OptionalInt.of(localDate.get(ChronoField.DAY_OF_WEEK)).ifPresent(System.out::println);

        //  时代(公元前、后) CE BCE
        Optional.of(localDate.getEra()).ifPresent(System.out::println);

        //  时代(公元前、后) 1--->CE 0--->BCE
        Optional.of(localDate.getEra().getValue()).ifPresent(System.out::println);
        OptionalInt.of(localDate.get(ChronoField.ERA)).ifPresent(System.out::println);

        //  ISO年表
        Optional.of(localDate.getChronology().getId()).ifPresent(System.out::println);

        //  当前时间
        LocalTime time = LocalTime.now();

        //   时
        OptionalInt.of(time.getHour()).ifPresent(System.out::println);
        OptionalInt.of(time.get(ChronoField.HOUR_OF_DAY)).ifPresent(System.out::println);

        //   分
        OptionalInt.of(time.getMinute()).ifPresent(System.out::println);
        OptionalInt.of(time.get(ChronoField.MINUTE_OF_DAY)).ifPresent(System.out::println);

        //   秒
        OptionalInt.of(time.getSecond()).ifPresent(System.out::println);
        OptionalInt.of(time.get(ChronoField.SECOND_OF_DAY)).ifPresent(System.out::println);

        //   纳秒
        OptionalInt.of(time.getNano()).ifPresent(System.out::println);
        OptionalLong.of(time.getLong(ChronoField.NANO_OF_SECOND)).ifPresent(System.out::println);

        //  中午时间
        Optional.of(LocalTime.NOON).ifPresent(System.out::println);

        //  午夜时间
        Optional.of(LocalTime.MIDNIGHT).ifPresent(System.out::println);

        //  自定义格式化时间
        DateTimeFormatter customDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss E");
        LocalDateTime localDateTime = LocalDateTime.of(localDate, time);
        Optional.of(localDateTime.format(customDateTimeFormatter)).ifPresent(System.out::println);

        //   根据传入的文本匹配自定义指定格式进行解析
        Optional.of(LocalDateTime.parse("2019-12-25 12:30:00 周三", customDateTimeFormatter))
                .ifPresent(System.out::println);

        //   时间点 Instant
        Instant start = Instant.now();
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant end = Instant.now();

        //    Duration 时间段
        Duration duration = Duration.between(start, end);
        OptionalLong.of(duration.toNanos()).ifPresent(System.out::println);

        //   Period  时间段
        Period period = Period.between(LocalDate.now(), localDate);

        OptionalInt.of(period.getYears()).ifPresent(System.out::println);
        OptionalInt.of(period.getMonths()).ifPresent(System.out::println);
        OptionalInt.of(period.getDays()).ifPresent(System.out::println);

        //   The Difference Between Duration And Period
        //   Durations and periods differ in their treatment of daylight savings time when added to ZonedDateTime.
        //   A Duration will add an exact number of seconds, thus a duration of one day is always exactly 24 hours.
        //   By contrast, a Period will add a conceptual day, trying to maintain the local time.

    }


}
四、备注
1、Why do we need a new date and time library?

https://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

2、java.time API

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73658.html

相关文章

  • Java8特性和Lambda达式

    摘要:表达式体现了函数式编程的思想,即一个函数亦可以作为另一个函数参数和返回值,使用了函数作参数返回值的函数被称为高阶函数。对流对象进行及早求值,返回值不在是一个对象。 Java8主要的改变是为集合框架增加了流的概念,提高了集合的抽象层次。相比于旧有框架直接操作数据的内部处理方式,流+高阶函数的外部处理方式对数据封装更好。同时流的概念使得对并发编程支持更强。 在语法上Java8提供了Lamb...

    gaara 评论0 收藏0
  • Java8新特性总览

    摘要:新特性总览标签本文主要介绍的新特性,包括表达式方法引用流默认方法组合式异步编程新的时间,等等各个方面。还有对应的和类型的函数连接字符串广义的归约汇总起始值,映射方法,二元结合二元结合。使用并行流时要注意避免共享可变状态。 Java8新特性总览 标签: java [TOC] 本文主要介绍 Java 8 的新特性,包括 Lambda 表达式、方法引用、流(Stream API)、默认方...

    mayaohua 评论0 收藏0
  • StreamLambda达式(五) Stream BaseStream AutoClose

    摘要:陈杨一流的定义流支持串行并行聚合操作元素序列二流的创建流的创建以方法生成流三 package com.java.design.java8.Stream.StreamDetail; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframe...

    HitenDev 评论0 收藏0
  • 《Java8实战》-第四章读书笔记(引入Stream

    摘要:内部迭代与使用迭代器显式迭代的集合不同,流的迭代操作是在背后进行的。流只能遍历一次请注意,和迭代器类似,流只能遍历一次。 流(Stream) 流是什么 流是Java API的新成员,它允许你以声明性方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现)。就现在来说,你可以把它们看成遍历数据集的高级迭代器。此外,流还可以透明地并行处理,你无需写任何多线程代码了!我会在后面的笔记中...

    _ivan 评论0 收藏0
  • Java实战之Java8指南

    摘要:首先我们定义一个有两个不同控制器的然后,我们创建一个特定的工厂接口来创建新的对象不需要手动的去继承实现该工厂接口,我们只需要将控制器的引用传递给该接口对象就好了的控制器会自动选择合适的构造器方法。这种指向时间轴的对象即是类。 本文为翻译文章,原文地址 这里 欢迎来到本人对于Java 8的系列介绍教程,本教程会引导你一步步领略最新的语法特性。通过一些简单的代码示例你即可以学到默认的接口方...

    nemo 评论0 收藏0

发表评论

0条评论

lidashuang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<