Compare two array lists of objects by property value

geralt93

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

Tushar

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']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to compare two Lists of Objects By Property Value

How to compare a property value in multiple objects of an array?

LINQ Compare two lists where property value is not equal

Compare two array of objects and get only the different Property of the array

How to compare two value that has same property of objects?

How to compare and filter objects of array based on property value?

Compare two array lists in java

Merge two array of objects based on a key value compare

How to compare two lists and change one property

How to compare a field between two Lists of objects?

Compare two lists of objects of the same type

How to compare two array of objects?

How to compare two array lists that consist of instances of objects that belong to the same superclass, but various subclasses.

Compare two arrays of objects and if value for a certain key is missing, add an object with that value to the array - two ways

Compare elements of two lists and calculate median value

Compare two array-containing lists in Python

Compare two lists of objects by name and get a list of different objects

Compare two values in an array of objects React and if the value (artistID) matches, display the objects with that ID

Compare two arrays of objects and remove items in the second one that have the same property value

Javascript Compare Property Value of 2 Arrays of Objects

Compare two lists of different objects by certain two fields

Javascript - How to compare property values in objects in an array

Compare two array by key and value

JavaScript compare two arrays and push items to the first array that have the same property value in the second array

How to compare values in an array, that is inside an array of objects, with a property of the objects?

Group objects in array by property value

How to compare two array of objects in query MongoDB

Compare two nested array and objects for find difference

Compare two array of objects based on their id's

TOP Ranking

HotTag

Archive