Currently I got this code:
@GetMapping("/all/")
public Iterable<Article> getAllArticle(){
ArrayList<ArticleEntity> articleEntities = Lists.newArrayList(articleProviderComponent.getAllArticle());
ArrayList<Article> articles = Lists.newArrayList();
for(ArticleEntity articleEntity : articleEntities){
articles.add(ArticleMapper.articleEntity2Article(articleEntity));
}
return articles;
}
The repository returns an Iterable
, which I want to convert to a ArrayList
. Beside that I want to convert each Entity
to a POJO
.
I tried using something like
list.foreach(ArticleMapper::ArticleMapper.articleEntity2Article)
which works fine, but does not return a new list.
A simple map
will do the job:
List<Article> articles = articleEntities.stream()
.map(ArticleMapper::articleEntity2Article)
.collect(Collectors.toList());
map
converts each element to something else using the method given.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With