stream常用操作

List<ProductApplyDetail> detailList = null;

1,取list对象属性

List<String> applyOrderNos = detailList.stream().map(v -> v.getApplyOrderNo()).collect(Collectors.toList());
List<ProductApplyOrder> orders = productApplyOrderMapper.selectByApplyOrderNos(applyOrderNos);
2,list转map
Map<String, ProductApplyOrder> orderMap = orders.stream().collect(Collectors.toMap(v -> v.getApplyOrderNo(), v -> v, (a, b) -> b));
3,循环赋值
detailList.stream().forEach(v -> v.setTitle(orderMap.containsKey(v.getApplyOrderNo()) ? orderMap.get(v.getApplyOrderNo()).getTitle() : ""));

2,list商品数据,根据商品编码分组,获取分组后第一个数据转map,源自导入商品基本信息

midBaseProductInfos=midBaseProductInfos
    .stream()
    .collect(
        Collectors
            .groupingBy(MidBaseProductInfo::getProductCode,LinkedHashMap::new,Collectors.toList())
    ).values().stream().map(list -> list.get(list.size() - 1)).collect(Collectors.toList());

本文列举了stream常用操作,可参考:https://www.cnblogs.com/jaycekon/p/7467026.html

主要方法:

filter:注意->操作函数返回一个boolean值

map

flatMap

代码

实体类

public class Dish {

    private String name;
    private boolean vegetarian;
    private int calories;
    private Type type;

    public enum Type {MEAT, FISH, OTHER}

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public void setVegetarian(boolean vegetarian) {
        this.vegetarian = vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Dish{" +
                "name='" + name + '\'' +
                ", vegetarian=" + vegetarian +
                ", calories=" + calories +
                ", type=" + type +
                '}';
    }
}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration
@Slf4j
public class StreamTest {

    static List<Dish> list = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH));

    public static void testForEach() {
        list.stream().forEach(dish ->
                dish.setName(dish.getName() + 1)
        );
        log.info("ForEach输出:数据:{},大小:{}", list.toArray(), list.size());
    }

    public static void testFilter() {
        List<Dish> listfilter = list.stream().filter(dish -> dish.getCalories() > 600).collect(Collectors.toList());
        log.info("Filter输出:数据:{},大小:{}", listfilter, listfilter.size());
    }

    public static void testFilterB() {
        List<Dish> listfilterB = list.stream().filter(Dish::isVegetarian).limit(3).collect(Collectors.toList());
        log.info("FilterB输出:数据:{},大小:{}", listfilterB, listfilterB.size());
    }


    public static List<String> testMap() {
        List<String> listMap = list.stream().map(Dish::getName).collect(Collectors.toList());
        log.info("Map输出:数据:{},大小:{}", listMap, list.size());
        return listMap;
    }

    public static void testFlatMap() {
        String[] words = new String[]{"shen", "ke"};
        List<String> uniqueCharacters = Arrays.asList(words).stream()
                .map(w -> w.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        log.info("testFlatMap:数据:{},大小:{}", uniqueCharacters, uniqueCharacters.size());
    }

    public static void main(String[] args) {
        testForEach();
        testFilter();
        testMap();
        testFilterB();
        testFlatMap();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值