Stream中间操作

authors.stream().map(new Function<Author, Integer>() {
    @Override
    public Integer apply(Author author) {
        return author.getAge()+10;
    }
}).forEach(System.out::println);

distinct

可以去除流中的重复元素

注意:distinct方法是依赖Object的equals方法来判断是否是相同对象的。所以需要注意equals方法。

sorted

重载的两个方法:

无参数sorted()

  • 需要实现Comparable接口
  • 重写compareTo方法
public class Author implements Comparable<Author>{
  
    @Override
    public int compareTo(Author o) {
        return this.getAge()-o.getAge();
    }

有参数Sorted()

public static void testThree(){
        List<Author> authors = getAuthors();
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge()-o2.getAge();
                    }
                })
                .forEach(author -> System.out.println(author.getAge()));

}

limit

可以设置流的最大长度,超出的部分将被抛弃

// 打印其中年龄最大的两个作家的姓名
authors.stream()
        .distinct()
        .sorted(new Comparator<Author>() {
            @Override
            public int compare(Author o1, Author o2) {
                return o2.getAge()-o1.getAge();
            }
        })
        .limit(2)
        .forEach(System.out::println);

skip

跳过流中的前n个元素,返回剩下的元素

flatMap

map只能把一个对象转成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素

可以把集合中的元素转为一个个对象类型来操作

List<Author> authors = getAuthors();
// 打印现有数据的所有分类
authors.stream()
        .flatMap( author -> author.getBooks().stream())
        .distinct()
        .flatMap( book -> Arrays.stream(book.getCategory().split(",")))
        .distinct()
        .forEach(System.out::println);

重载的两个方法:

无参数sorted()

  • 需要实现Comparable接口
  • 重写compareTo方法
public class Author implements Comparable<Author>{
  
    @Override
    public int compareTo(Author o) {
        return this.getAge()-o.getAge();
    }

有参数Sorted()

public static void testThree(){
        List<Author> authors = getAuthors();
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge()-o2.getAge();
                    }
                })
                .forEach(author -> System.out.println(author.getAge()));

}

limit

可以设置流的最大长度,超出的部分将被抛弃

// 打印其中年龄最大的两个作家的姓名
authors.stream()
        .distinct()
        .sorted(new Comparator<Author>() {
            @Override
            public int compare(Author o1, Author o2) {
                return o2.getAge()-o1.getAge();
            }
        })
        .limit(2)
        .forEach(System.out::println);

skip

跳过流中的前n个元素,返回剩下的元素

flatMap

map只能把一个对象转成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素

可以把集合中的元素转为一个个对象类型来操作

List<Author> authors = getAuthors();
// 打印现有数据的所有分类
authors.stream()
        .flatMap( author -> author.getBooks().stream())
        .distinct()
        .flatMap( book -> Arrays.stream(book.getCategory().split(",")))
        .distinct()
        .forEach(System.out::println);

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值