0

I have two objects that have two fields in common, code field and position field. I use two distinct lists, one of Object1 and another of Object2. Object2 was filtered before and had missing elements compared to Object1. I need to stream() on Object1 to compare code value and position value of objects that are still in the list of Object2 to keep them in the list of Object1.

Example:

//List<Object1> object1 :
object1.get(0) // code="VALUE1"; position=1; other fields...
object1.get(1) // code="VALUE2"; position=2; other fields...
object1.get(2) // code="VALUE3"; position=3; other fields...

//List<Object2> object2 :
object2.get(0) // code="VALUE2"; position=2; other fields...
object2.get(1) // code="VALUE3"; position=3; other fields...

object1 = object1.stream()...().collect(Collectors.toList()); // After object1 should have :
object1.get(0) // code="VALUE2"; position=2; other fields...
object1.get(1) // code="VALUE3"; position=3; other fields...

How can I do this with Java 8 Stream API?

3
  • 1
    Compare and do what? Are Object1 and Object2 related in any way? Commented Nov 12, 2021 at 17:49
  • 2
    What are you trying to achieve? Why do you think map() is a good approach? If you want to compare similar objects based on their code and position, a better data structure than lists would be a map from the code and position to the objects. You could create a data type that just represents a code and position pair, and then use it to look up the objects with those values directly, assuming the code and position are unique and no two objects ever have the same values. Commented Nov 12, 2021 at 17:49
  • My lists are coming from two differents part of the application and are not using for the same thing at all. I need to assemble them and pass them to another operating part of the system. You probably right, create a Map (Map<Object1,Object2>) is probably a better solution. Thanks Commented Nov 12, 2021 at 18:26

1 Answer 1

1

If I understood your description correctly the following should do it:

public static void main (String[] args) {
    List<Object1> object1 = new ArrayList<>();
    object1.add(new Object1("1", 1));
    object1.add(new Object1("2", 2));
    object1.add(new Object1("3", 3));
    object1.add(new Object1("4", 4));
    object1.add(new Object1("5", 5));

    List<Object2> object2 = new ArrayList<>();
    object2.add(new Object2("1", 1));
    object2.add(new Object2("2", 2));
    object2.add(new Object2("5", 5));

    List<Object1> filteredObject1 = object1.stream()
            .filter(o1 -> object2.stream().anyMatch(o2 -> o2.getCode().equals(o1.getCode()) && o2.getPosition() == o1.getPosition()))
                    .collect(Collectors.toList());

    System.out.println(filteredObject1);
}

It keeps in object1 the Object1 instances that have the same code and position of an Object2 instance in object2.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I wanted. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.