0

I'm facing a problem in Java... I have one list of object Objeto, this object has the attributes listed below:

public class Objeto{
    private FirstEntity first;
    private List<ThirdEntity> thirds;
}

I need to find the objects in this list that have the same FirstEntity attribute... How can I do this?

Thanks in advance!

8
  • Override equals() method in the object. Commented May 2, 2017 at 17:42
  • 2
    What have you tried? You've got to put forth a little effort. Are instances of FirstEntity realy the "same" or do they just have the same contents? Commented May 2, 2017 at 17:42
  • @ChristopherSchneider they just have the "same" content Commented May 2, 2017 at 17:43
  • @PritamBanerjee how do you sugest to override equals? Commented May 2, 2017 at 17:45
  • 1
    Do you have FirstEntity object to compare with the attributes from the list? Or you want to find all coinsidents from the list without any template FirstEntity object? Commented May 2, 2017 at 17:49

2 Answers 2

3

The main reason why this question is difficult to answer, is because we don't know how many possible values there are for FirstEntity to hold. For this reason, we must use a Map<FirstEntity, List<Objecto>> where, for every FirstEntity, we store a List<Objecto> that share the FirstEntity attribute.

For this to compile, you must create a getter in your Objecto class for FirstEntity:

public FirstEntity getFirstEntity() {
    return first;
}

Then, the List<Objecto> can be streamed and collected into a Map<FirstEntity, List<Objecto>>:

Map<FirstEntity, List<Objecto>> map = thirds.stream().collect(Collectors.groupingBy(Objecto::getFirstEntity));

For this to work, you must override Object#equals and Object#hashCode within FirstEntity.

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

4 Comments

Thanks! But I cannot remove the elements... I have to edit the duplicated elements...
You're welcome! I've edited my post to accommodate that.
Thanks for your answer! But that's not the right object... I've edited my question to illustrate better... Thanks! =)
OH that makes more sense, I'll fix it again!
2

(As before, if you need to use Java 7 or earlier - and same discussion re. FirstEntity hashCode() and equals() as in the other answer).

Assuming we have

 List<Objecto> myObjectos;

Then the following should work:

 Map<FirstEntity, List<Objecto>> map = new HashMap<>();
 for(Objecto objecto : myObjectos){
       if(!map.containsKey(objecto.first)){
              map.put(objecto.first, new LinkedList<Objecto>());
       }
       map.get(objecto.first).add(objecto);
 }

5 Comments

Thanks for your answer! But that's not the right object... I've edited my question to illustrate better... Thanks! =)
Not sure I understand - are you asking, for all Objectos on a List, L, which of them have a first which is another Objecto (on the list) already has? Or are you asking to "group" all Objecto's which share "first"?
If you don't need objetos with unique attribute, just remove from the map all keys with values of size < 2.
@dan.mwasuser2321368 your code in for loop can be simplified to:map.computeIfAbsent(objecto.first, (key)-> new LinkedList<>()).add(objecto)
@holi-java my answer explicitly states that it is for the case where the user is limited to Java 7. Jacob G,'s answer contains a Java 8 solution.

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.