Beautify for loop using stream and lambda

elp :

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.

Sweeper :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=72151&siteId=1