Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 Streams - Compare Two List's object values and add value to sub object of first list?

I have two classes:

public class ClassOne {

 private String id;
 private String name;
 private String school;
 private String score; //Default score is null

 ..getters and setters..
}

public class ClassTwo {

 private String id;
 private String marks; 

 ..getters and setters..
}

And, I have two Lists of the above classes,

List<ClassOne> listOne;
List<ClassTwo> listTwo;

How can I compare two list and assign marks from listTwo to score of listOne based on the criteria if the IDs are equal. I know, we can use two for loops and do it. But I want to implement it using Java8 streams.

List<ClassOne> result = new ArrayList<>();

for(ClassOne one : listOne) {
    for(ClassTwo two : listTwo) {
        if(one.getId().equals(two.getId())) {
            one.setScore(two.getmarks());
            result.add(one);
        }
    }
}
return result;

How can I implement this using Java8 lambda and streams?

like image 540
Arpit Mathur Avatar asked Feb 17 '18 01:02

Arpit Mathur


People also ask

How do I compare one list with another list in Java 8?

Java provides a method for comparing two Array List. The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

How do you compare two Arraylists that are having the same object?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

How do you filter a list of objects using a Stream?

Java stream provides a method filter() to filter stream elements on the basis of given predicate. Suppose you want to get only even elements of your list then you can do this easily with the help of filter method. This method takes predicate as an argument and returns a stream of consisting of resulted elements.


2 Answers

Following code copies marks from ObjectTwo to score in ObjectOne, if both ids are equal, it doesn't have intermediate object List<ObjectOne> result

listOne.stream()
    .forEach(one -> {listTwo.stream()
        .filter(two -> {return two.getId().equals(one.getId());})
        .limit(1)
        .forEach(two -> {one.setScore(two.getMarks());});
    });
like image 157
Pavan Kumar Jorrigala Avatar answered Nov 21 '22 17:11

Pavan Kumar Jorrigala


Let listOne.size() is N and listTwo.size() is M. Then 2-for-loops solution has complexity of O(M*N).

We can reduce it to O(M+N) by indexing listTwo by ids.

Case 1 - assuming listTwo has no objects with the same id

// pair each id with its marks
Map<String, String> marksIndex = listTwo.stream().collect(Collectors.toMap(ObjectTwo::getId, ObjectTwo::getMarks));
// go through list of `ObjectOne`s and lookup marks in the index
listOne.forEach(o1 -> o1.setScore(marksIndex.get(o1.getId())));

Case 2 - assuming listTwo has objects with the same id

    final Map<String, List<ObjectTwo>> marksIndex = listTwo.stream()
            .collect(Collectors.groupingBy(ObjectTwo::getId, Collectors.toList()));

    final List<ObjectOne> result = listOne.stream()
            .flatMap(o1 -> marksIndex.get(o1.getId()).stream().map(o2 -> {
                // make a copy of ObjectOne instance to avoid overwriting scores
                ObjectOne copy = copy(o1);
                copy.setScore(o2.getMarks());
                return copy;
            }))
            .collect(Collectors.toList());

To implement copy method you either need to create a new object and copy fields one by one, but in such cases I prefer to follow the Builder pattern. It also results in more "functional" code.

like image 29
Devstr Avatar answered Nov 21 '22 18:11

Devstr