简体   繁体   中英

compare ArrayList with contain object

I have my object class:

public class Test {
private String name;
private int id;
private boolean aBoolean;

public Test(String name, int id, boolean aBoolean) {
    this.name = name;
    this.id = id;
    this.aBoolean = aBoolean;
}

and 2 arrayList of this object:

 ArrayList<Test> myFirstList = new ArrayList<>();
    ArrayList<Test> mySecondList = new ArrayList<>();

    myFirstList.add(new Test("a", 1, false));
    myFirstList.add(new Test("b", 2, false));
    myFirstList.add(new Test("c", 3, false));

    mySecondList.add(new Test("a", 1, false));
    mySecondList.add(new Test("b", 2, false));
    mySecondList.add(new Test("c", 3, false));

now I need to check that these array list contain objects which have same fields inside; is there any ways but to use fori loop and getting each parameter for compare?

Record

Apparently you want to compare objects of the same class for equality by examining the content of each and every member field.

You get that behavior automatically by using the records feature . This feature is new in Java 16, with early access builds available now. In a record you simply declare the member fields. The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .

So no need for you to override equals wih your own implementation as seen in the other Answers.

Use a record where the primary purpose of a class is to immutably and transparently carry data. In contrast, if your class has a focus on behavior with encapsulated data, or uses other OOP features such as inheritance, then your should use a regular class, not records.

So your code:

public class Test {
private String name;
private int id;
private boolean aBoolean;

public Test(String name, int id, boolean aBoolean) {
    this.name = name;
    this.id = id;
    this.aBoolean = aBoolean;
}

… becomes:

public record Test ( String name , int id , boolean aBoolean ) {}

Using that record:

Test x = new Test ( "Alice" , 42 , true ) ;
Test y = new Test ( "Alice" , 42 , false ) ;
boolean same = x.equals( y ) ;  // false because of third field.

Comparing lists

Use List#equals to see if two lists contain equal elements in the same order.

List x = List.of(
    new Test ( "Alice" , 1 , true ) ,
    new Test ( "Bob" , 2 , true ) ,
    new Test ( "Carol" , 3 , true ) 
);

List y = List.of(
    new Test ( "Alice" , 1 , true ) ,
    new Test ( "Bob" , 2 , true ) ,
    new Test ( "Carol" , 3 , true ) 
);
boolean same = x.equals( y ) ;   // True. 

If you override equals() in our class you should be able to sort both lists with myFirstList.sort() and then just use myFirstList.equals(mySecondList)

This is not related to ArrayList, it is related to your Test Class. As said by @VGR, In order to know if the two objects of the Test Class have the same fields, you have to override the equals method inside your Test class like this:

public class Test {

    // your constructors and methods
    
    @Override
    public boolean equals(Object o) { 
     
        if (o == this) { 
            return true; 
        } 

        if (!(o instanceof Test)) { 
            return false; 
        } 
            
        Test t = (Test) o; 
          
        // Compare the data members and return accordingly  
        return this.name.equals(t.name)
                && this.id == t.id;
                && this.aBoolean == t.aBoolean; 
    } 
}

Now to check if two objects in ArrayList are equal, Simply do this:

    boolean result = myFirstList.get(indexOfObject1).equals(indexOfObject2);
    System.out.println("Result : "+result);

And obviously, to search for equal objects in two ArrayLists, you have to loop through every single object in the first list, then compare that object with all objects in the second list, or implement your own algorithm. That's where data structure and algorithms are used!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM