java8新特性学习——StreamAPI使用学习下(四)

本文介绍 Java 中 Stream API 的实际应用案例,包括流排序、计数、查找最大最小值等操作,并展示了如何自定义排序规则及对数字进行比较。
/**
 * 流排序
 */
@Test
public void test3(){
    List<String> list = Arrays.asList("q", "d", "r", "s");
    list.stream()
            .sorted()  //默认升序
            .forEach(System.out::println);
}


@Test
public void test4(){
    List<Book> books = Arrays.asList(
            new Book("1", "a", "1", "1", 1, new Date()),
            new Book("2", "b", "2", "2", 2, new Date()),
            new Book("3", "c", "3", "3", 1, new Date()),
            new Book("4", "d", "4", "4", 4, new Date())

    );
    books.stream()
            .sorted((x,y) -> y.getAge() - x.getAge())   //小 - 大  升序 |  大 - 小  降序
            .forEach(System.out::println);
}

//如果某字段相同 自定义排序  不同 按原先定义的排序
@Test
public void test5(){
    List<Book> books = Arrays.asList(
            new Book("1", "a", "1", "1", 1, new Date()),
            new Book("2", "b", "2", "2", 2, new Date()),
            new Book("3", "c", "3", "3", 1, new Date()),
            new Book("4", "d", "4", "4", 4, new Date())

    );
    books.stream()
            .sorted((x,y) ->{
            return   x.getAge() - y.getAge() == 0 ? x.getName().compareTo(y.getName()) : x.getAge().compareTo(y.getAge());
    }).forEach(System.out::println);
}


// 数字的操作——通过流 获取 count max min
@Test
public void test6(){
    List<Book> books = Arrays.asList(
            new Book("1", "a", "1", "1", 1, new Date()),
            new Book("2", "b", "2", "2", 2, new Date()),
            new Book("3", "c", "3", "3", 1, new Date()),
            new Book("4", "d", "4", "4", 4, new Date())

    );

    long count = books.stream().count();
    System.out.println("个数" + count);
    Optional<Book> max = books.stream().max((x, y) -> Integer.compare(x.getAge(), y.getAge()));
    System.out.println("最大"+max.get().getAge());
    Optional<Book> min = books.stream().min((x, y) -> Integer.compare(x.getAge(), y.getAge()));
    System.out.println("最小"+min.get().getAge());
}

// 直接对数字进行比较
@Test
public void test7(){
    List<Integer> integers = Arrays.asList(1, 3, 4, 5, 12, 2, 42, 13, 0);
    long count = integers.stream().count();
    System.out.println("个数" + count);
    Optional<Integer> max = integers.stream().max((x, y) -> Integer.compare(x, y));
    System.out.println("最大"+max);
    Optional<Integer> min = integers.stream().min((x, y) -> Integer.compare(x, y));
    System.out.println("最小"+min);
}

//另一种方式比较
// 直接对数字进行比较
//对于对象中的数字类型的属性也可以使用 Integer::compare 方法
@Test
public void test8(){
    List<Integer> integers = Arrays.asList(1, 3, 4, 5, 12, 2, 42, 13, 0);

    Optional<Integer> min = integers.stream().min(Integer::compare);
    System.out.println("最小"+min);

    Optional<Integer> max = integers.stream().max(Integer::compare);
    System.out.println("最大"+max);
}


未完待续 。。。。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值