Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

java-8

Currently I have list of object array from that array i have to Iterate and add to the list of my LatestNewsDTO what i have done below code working but still i am not satisfy with my way . Is their any efficient way please let me know.

Thanks

List<Object[]> latestNewses = latestNewsService.getTopNRecords(companyId, false, 3);
List<LatestNewsDTO> latestNewsList = new ArrayList();
latestNewses.forEach(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]);
    latestNewsList.add(latestNews); 
});
like image 614
soorapadman Avatar asked Mar 04 '16 07:03

soorapadman


People also ask

How an iterate object can be used to iterate a list in Java?

By using this iterator object, you can access each element in the collection, one element at a time. Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.

How do I traverse a list in Java 8?

Since Java 8, we can use the forEach() method to iterate over the elements of a list. This method is defined in the Iterable interface, and can accept Lambda expressions as a parameter.

How do you traverse an object array in Java?

You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.

How can I turn a list of lists into a list in Java 8?

Here is the simple, concise code to perform the task. // listOfLists is a List<List<Object>>. List<Object> result = new ArrayList<>(); listOfLists. forEach(result::addAll);


1 Answers

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.

like image 60
Eran Avatar answered Oct 06 '22 10:10

Eran