How to iterate List of object array and set to another object list in java 8?

Use a Stream to map your Object[] arrays to LatestNewsDTOs and collect them into a List :

List<LatestNewsDTO> latestNewsList =
    latestNewses.stream()
                .map(objects -> {
                    LatestNewsDTO latestNews = new LatestNewsDTO();
                    latestNews.setId(((BigInteger) objects[0]).intValue());
                    latestNews.setCreatedOn((Date) objects[1]);
                    latestNews.setHeadLine((String) objects[2]);
                    latestNews.setContent(((Object) objects[3]).toString());
                    latestNews.setType((String) objects[4]);
                    return latestNews;
                })
                .collect(Collectors.toList());

Of course, if you create a constructor of LatestNewsDTO that accepts the the array, the code will look more elegant.

List<LatestNewsDTO> latestNewsList =
    latestNewses.stream()
                .map(objects -> new LatestNewsDTO(objects))
                .collect(Collectors.toList());

Now the LatestNewsDTO (Object[] objects) constructor can hold the logic that parses the array and sets the members of your instance.

Tags:

Java

Java 8