0

I have two array lists of objects, I need to compare them by the property value and to return unique. Object has two properties, question and answer, only the objects from list one with unique question should be returned.

        *faq(question, answer)*

        List<faq> one = new ArrayList<>();
        one.add(new faq("question 1", "answer 1"));
        one.add(new faq("question 2", "answer 2"));
        one.add(new faq("question 3", "answer 3"));
        
        List<faq> two = new ArrayList<>();
        two.add(new faq("question 4", "answer 4"));
        two.add(new faq("question 5", "answer 5"));
        two.add(new faq("question 1", "answer 6"));
        two.add(new faq("question 7", "answer 7"));
        two.add(new faq("question 8", "answer 8"));

From this code here I want to get objects from list one with questions 2 and 3 since only those two are not contained in list two

2
  • 1
    Does the class faq implement/overrides the equals (and hashcode) method? Commented Dec 3, 2020 at 15:12
  • Are you looking for List#removeAll method? javadocs If you want to leave list one without changes, just copy it first. Commented Dec 3, 2020 at 15:22

1 Answer 1

1

You need to implement the equals() and hashCode() in Faq.java

  • Faq.java
    import java.util.Objects;
    import java.util.StringJoiner;
    
    public class Faq {
        private String questions;
        private String answer;
    
        public Faq(final String questions, final String answer) {
            this.questions = questions;
            this.answer = answer;
        }
    
    
        @Override
        public boolean equals(final Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            final Faq faq = (Faq) o;
            return questions.equals(faq.questions);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(questions);
        }
    
        @Override
        public String toString() {
            return new StringJoiner(", ", Faq.class.getSimpleName() + "[", "]")
                    .add("questions='" + questions + "'")
                    .toString();
        }
    }
  • Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Faq> one = new ArrayList<>();
        one.add(new Faq("question 1", "answer 1"));
        one.add(new Faq("question 2", "answer 2"));
        one.add(new Faq("question 3", "answer 3"));

        List<Faq> two = new ArrayList<>();
        two.add(new Faq("question 4", "answer 4"));
        two.add(new Faq("question 5", "answer 5"));
        two.add(new Faq("question 1", "answer 6"));
        two.add(new Faq("question 7", "answer 7"));
        two.add(new Faq("question 8", "answer 8"));

        one.removeAll(two);
        System.out.println(one);
    }
}
  • Output
    [Faq[questions='question 2'], Faq[questions='question 3']]
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting that it modifies list one. If OP wants to leave one as is, he should copy it first.

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.