Use Stream to map Object[] arrays to LatestNewsDTO and assemble 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 the LatestNewsDTO constructor that takes an 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 contain logic that parses the array and sets the members of your instance.
source share