Java 8 stream API- Filter based on condition and collect the object

前端 未结 2 1390
花落未央
花落未央 2021-01-06 06:28

In java 8 , collect emp object based on some filter condition.

in main class

List empList = Arrays.asList(
    new Emp(\"aaa\", languag         


        
相关标签:
2条回答
  • 2021-01-06 07:17

    You can also do like this.

    List<Object> optionMetas = new ArrayList<>();
    Map<Long, Object> optionIdMetaMap_ = optionMetas.stream().filter(option -> option.getXX() || option.getXXX().equal("java"))
                                .collect(Collectors.toMap(Object::getKEY, item-> item));
    

    Add your relevant condition in filter()

    0 讨论(0)
  • 2021-01-06 07:25

    You should not use flatMap if you want to collect Emp objects in the end because it will change every element to something else and it can be quite hard to map them back.

    You should put all your logic in a filter: "keep the Emp object if getLanguage contains "java"".

    empList.stream()
        .filter(x->x.getLanguage().contains("java"))
        .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题