Home > OS >  how to get List<String> of value from key:value return by mongo in java
how to get List<String> of value from key:value return by mongo in java

Time:09-15

I am using Mongo crud repository in java spring boot project, for entity like id:"1",id:"2",id:"3", I want to return List of String like ["1","2","3"] I am using findAllById and response is {id:1,id:2,id:3} I converted it into ["1","2","3"] by using loop and then return list.

for(Entity entity:entity.findAllById()) {
        list.add(store.getId());
    }

is this possible without using this loop I can do the same and return string list .

CodePudding user response:

org.apache.commons.collections4.IterableUtils.toList(repo.findAllById(ids)). stream().map(Entity::getId).collect(Collectors.toList())

Note the IterableUtils.toList has a loop :)

  • Related