Java流操作常用函数总结(持续更新中)

1、stream()后可调函数

方法名说明
filter()过滤掉一部分元素,剩下符合条件的元素
limit()取前几个元素,括号里填数字
map()取实体类里的字段数据
reduce()归约操作可以将流中元素反复结合起来,得到一个值
distinct()去除重复元素
sorted()排序

2、举例说明

实体类Phone

public class Phone {

    //品牌
    private String brand;

    //价格
    private Integer price;

    public Phone() {
    }

    public Phone(String brand, Integer price) {
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}

测试类TestStream

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class TestStream {

    public static void main(String[] args) {

        Phone phone1 = new Phone("OPPO",2999);
        Phone phone2 = new Phone("VIVO",2599);
        Phone phone3 = new Phone("华为",4999);
        Phone phone4 = new Phone("小米",1999);
        Phone phone5 = new Phone("魅族",2399);

        List<Phone> phoneList = new ArrayList<>();
        phoneList.add(phone1);
        phoneList.add(phone2);
        phoneList.add(phone3);
        phoneList.add(phone4);
        phoneList.add(phone5);

//        //验证filter(),筛选手机价格大于2600的手机,输出OPPO,华为
//        phoneList = phoneList.stream().filter(p -> p.getPrice() > 2600).collect(Collectors.toList());

//        //验证limit(),截取前3条数据,输出OPPO,VIVO,华为
//        phoneList = phoneList.stream().limit(3).collect(Collectors.toList());

//        //验证filter(),limit()连用,输出OPPO,VIVO
//        phoneList = phoneList.stream().filter(p -> p.getPrice() > 2000).limit(2).collect(Collectors.toList());

//        //验证map取实体里字段值,输出2999,2599,4999,1999,2399  (下面是两种用法,结果相同)
//        List<Integer> priceList = phoneList.stream().map(p -> p.getPrice()).collect(Collectors.toList());
//        List<Integer> priceList = phoneList.stream().map(Phone::getPrice).collect(Collectors.toList());
//        priceList.forEach(p -> System.out.println(p));

//        //验证reduce(),求价格总和,输出14995,orElse是当list里没有元素时,输出0
//        Integer totalPrice = phoneList.stream().map(Phone::getPrice).reduce((a, b) -> a + b).orElse(0);
//        System.out.println(totalPrice);

//        //验证distinct(),去重,输出1,3,2
//        List<String> StringList = new ArrayList<>();
//        StringList.add("1");
//        StringList.add("3");
//        StringList.add("2");
//        StringList.add("3");
//        StringList.add("1");
//        StringList = StringList.stream().distinct().collect(Collectors.toList());
//        StringList.forEach(p -> System.out.println(p));

//        //验证sorted(),根据price从小到大
//        phoneList = phoneList.stream().sorted((a,b) -> {
//            return a.getPrice().compareTo(b.getPrice());
//        }).collect(Collectors.toList());

//        //验证sorted(),根据price从大到小
//        phoneList = phoneList.stream().sorted((a,b) -> {
//            return b.getPrice().compareTo(a.getPrice());
//        }).collect(Collectors.toList());

//        //验证sorted(),根据price从小到大
//        phoneList = phoneList.stream().sorted(Comparator.comparing(Phone::getPrice)).collect(Collectors.toList());

//        //验证sorted(),根据price从大到小
//        phoneList = phoneList.stream().sorted(Comparator.comparing(Phone::getPrice).reversed()).collect(Collectors.toList());

//        //验证sorted(),自然排序,输出[1, 3, 5, 6, 7]
//        List<Integer> integerList = new ArrayList<>();
//        integerList.add(5);
//        integerList.add(7);
//        integerList.add(1);
//        integerList.add(3);
//        integerList.add(6);
//        integerList = integerList.stream().sorted().collect(Collectors.toList());
//        System.out.println(integerList);
//
//        //验证sorted(),从大到小排序,输出[7, 6, 5, 3, 1]
//        integerList = integerList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
//        System.out.println(integerList);

        phoneList.forEach(p -> System.out.println(p.getBrand()));

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值