Stream流

代码里面有相关命令的解释--直接放在项目里面运行即可. 

package com.gps.stream;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.stream.Collectors;


public class Demo {

    public static void main(String[] args) {
//test01();
//test02();
//test03();
//test04();
//test05();
//test06();
//test07();
//test08();
//test09();
//test10();
//        test11();
//        test12();
//        test13();
//        test14();
//        test15();
//        test16();
//        test17();
//        test18();
//        test19();
//        test20();
        test21();

    }

    /**
     * 使用reduce求所有作者中年龄的最大值
     */
    private static void test21() {
        ArrayList<Author> authors = author();
        Integer reduce = authors.stream()
                .map(Author::getAge)
                .reduce(Integer.MAX_VALUE, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer result, Integer integer) {
                        return result < integer ? result : integer;
                    }
                });
        System.out.println(reduce);
    }

    /**
     * 使用reduce求所有作者中年龄的最大值
     */
    private static void test20() {
        ArrayList<Author> authors = author();
        Integer reduce = authors.stream()
                .map(Author::getAge)
                .reduce(Integer.MIN_VALUE, (result, integer) -> result < integer ? integer : result);
        System.out.println(reduce);


    }

    /**
     * 使用reduce求所有作者年龄的和
     * reduce 用来计算
     */
    private static void test19() {
        ArrayList<Author> authors = author();
        Integer reduce = authors.stream()
                .distinct()
                .map(Author::getAge)
                .reduce(0, Integer::sum);
        System.out.println(reduce);
    }

    /**
     * 获取一个年龄最小的作家,并输出他的姓名
     */
    private static void test18() {
        ArrayList<Author> authors = author();
        Optional<Author> first = authors.stream()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge() - o2.getAge();
                    }
                })
                .findFirst();
        first.ifPresent(author -> System.out.println(author.getName()));
    }

    /**
     * 获取任意一个年龄大于18的作家,如果存在就输出他的名字
     */
    private static void test17() {
        ArrayList<Author> authors = author();
        Optional<Author> optional = authors.stream()
                .filter(author -> author.getAge() > 18)
                .findAny();
        optional.ifPresent(author -> System.out.println(author.getAge()));

    }

    /**
     * 判断是否所有的作家是不是成年人
     */
    private static void test16() {
        ArrayList<Author> authors = author();
        boolean b = authors.stream().noneMatch(author ->
                author.getAge() > 18
        );
        System.out.println(b);
    }

    /**
     * 判断是否所有的作家都是成年人
     */
    private static void test15() {
        ArrayList<Author> authors = author();
        boolean b = authors.stream().allMatch(author ->
                author.getAge() >= 18
        );
        System.out.println(b);
    }

    /**
     * 判断是否有年龄在29以上的作家
     */
    private static void test14() {
        ArrayList<Author> authors = author();
        boolean b = authors.stream().anyMatch(author -> author.getAge() > 29);
        System.out.println(b);
    }

    /**
     * 获取一个Map集合,map的key为作者名,value为List<Book>
     */
    private static void test13() {
        ArrayList<Author> authors = author();
        Map<String, List<Book>> map = authors.stream()
                .distinct()
                .collect(Collectors.toMap(Author::getName, Author::getBooks));
        System.out.println(map);
    }

    /**
     * 获取一个所有书名的Set集合
     */
    private static void test12() {
        ArrayList<Author> authors = author();

//        Set<String> set = authors.stream()
//                .flatMap(author -> author.getBooks().stream())
//                .map(Book::getName)
//                .collect(Collectors.toSet());

        Set<Book> set = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .collect(Collectors.toSet());
        System.out.println(set);

    }

    /**
     * 获取一个存放所有作者名字的List集合
     */
    private static void test11() {
        ArrayList<Author> authors = author();
        List<String> list = authors.stream()
                .map(Author::getName)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(list);
    }

    /**
     * //        分别获取这些作家的所出书籍的最高分和最低分并打印
     */
    private static void test10() {

        ArrayList<Author> authors = author();
        System.out.println("最大值:" + authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(Book::getScore)
                .max((o1, o2) -> o1 - o2).get());
        System.out.println("最小值:" + authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .map(Book::getScore)
                .min((o1, o2) -> o1 - o2).get());
    }


    /**
     * 打印这些作家的所出书籍的数目,注意删除重复元素。
     */
    private static void test09() {
        ArrayList<Author> authors = author();
        System.out.println(authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .count());

    }

    /**
     * 打印现有数据的所有分类。要求对分类进行去重。不能出现这种格式:哲学,爱情爱情
     */
    private static void test08() {
        ArrayList<Author> authors = author();
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()//对所有的书籍先进行一步去重
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()//对书中的分类进行去重
                .forEach(System.out::println);


    }

    /**
     * 打印所有书籍的名字。要求对重复的元素进行去重
     * <p>
     * flatMap:  将一个对象类型 转换为 多个对象类型
     */
    private static void test07() {
        ArrayList<Author> authors = author();
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 打印除了年龄最大的作家外的其他作家,要求不能有重复元素,并且按照年龄降序排序。
     */
    private static void test06() {
//        打印除了年龄最大的作家外的其他作家,要求不能有重复元素,并且按照年龄降序排序。
        ArrayList<Author> authors = author();
        authors.stream()
                .distinct()
                .sorted()
                .skip(1)
                .map(Author::getName)
                .forEach(System.out::println);
    }

    private static void test05() {
        //根据年龄对作者进行降序排序,并去重
        ArrayList<Author> authors = author();
        //方法1
        authors.stream()
                .distinct()
                .sorted()
                .map(Author::getAge)
                .forEach(System.out::println);
        //方法2
        authors.stream()
                .distinct()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .map(Author::getAge)
                .forEach(System.out::println);

    }

    /**
     * distinct  去重
     */
    private static void test04() {
        ArrayList<Author> authors = author();
        authors.stream()
                .distinct()
                .map(Author::getName)
                .forEach(System.out::println);
    }

    /**
     * map操作:
     * map 可以将类型作为转换   之前每个对象都是Author类型  调用map后 得到的对象就是String类型
     */
    private static void test03() {
        //打印所有作者的名称
        ArrayList<Author> authors = author();
        authors.stream()
                .map(Author::getName)//
                .forEach(System.out::println);
    }

    /**
     * 查询作者年龄小于18岁的名称,并且去重
     * filter 用法
     */
    private static void test02() {
        ArrayList<Author> authors = author();
//        查询作者年龄小于18岁的名称,并且去重
        authors.stream()//进入流式编程
                .filter(author -> author.getAge() < 18)//进行对作者年龄过滤
                .distinct()//去重
                .forEach(author -> System.out.println(author.getName()));//然后将过滤后的结果进行输出打印
    }

    /**
     * 需要将map双列集合转为单列集合(实际就是把最外层 "{"  转为:"[") 才能进行stream流操作
     */
    private static void test01() {
        Map<String, Integer> map = new HashMap<>();
        map.put("蜡笔小新", 19);
        map.put("黑子", 17);
        map.put("日向翔阳", 16);
        //需要将map双列集合转为单列集合(实际就是把最外层 "{"  转为:"[") 才能进行stream流操作
        /*    System.out.println(map);
            System.out.println(map.entrySet());*/

        map.entrySet().stream().filter(entry -> entry.getValue() > 16).forEach(System.out::println);
    }

    private static ArrayList<Author> author() {
        Book book1 = new Book(1L, "狼王梦1", "儿童,哲学", 8, "快乐1");
        Book book2 = new Book(2L, "狼王梦2", "成人,教育", 7, "快乐2");
        Book book3 = new Book(3L, "狼王梦3", "青年,教育", 6, "快乐3");
        Book book4 = new Book(4L, "狼王梦4", "老年,儿童,休闲", 9, "快乐4");
        List<Book> bookList1 = new ArrayList<>();
        List<Book> bookList2 = new ArrayList<>();
        List<Book> bookList3 = new ArrayList<>();
        List<Book> bookList4 = new ArrayList<>();
        bookList1.add(book1);
        bookList2.add(book2);
        bookList3.add(book3);
        bookList4.add(book4);

        Author author1 = new Author(1L, "高鹏帅1", 155, "快乐", bookList1);
        Author author2 = new Author(2L, "高鹏帅2", 12, "快乐", bookList2);
        Author author3 = new Author(3L, "高鹏帅3", 20, "快乐", bookList3);
        Author author4 = new Author(3L, "高鹏帅4", 20, "快乐", bookList4);

        ArrayList<Author> authors = new ArrayList<>();
        authors.add(author1);
        authors.add(author2);
        authors.add(author3);
        authors.add(author4);
        return authors;
    }


}
Author类:
package com.gps.stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author implements Comparable<Author>{
    //id
    private Long id;
    //姓名
    private String name;
    // 年龄
    private Integer age;
    // 简介
    private String intro;
    // 作品
    private List<Book> books;


    @Override
    public int compareTo(Author o) {
        return o.age-this.age;
    }
}

Book类
package com.gps.stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
//用于后期的去重使用
public class Book {
    private Long id;
    //书名
    private String name;
    //分类
    private String category;
    //评分
    private Integer score;
    // 简介
    private String intro;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小高求学之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值