简体   繁体   English

比较 ArrayList 与包含 object

[英]compare ArrayList with contain object

I have my object class:我有我的 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:和此 object 的 2 个 arrayList:

 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?除了使用fori循环并获取每个参数进行比较之外,还有其他方法吗?

Record记录

Apparently you want to compare objects of the same class for equality by examining the content of each and every member field.显然,您想通过检查每个成员字段的内容来比较相同 class 的对象是否相等。

You get that behavior automatically by using the records feature .您可以通过使用记录功能自动获得该行为。 This feature is new in Java 16, with early access builds available now.此功能是 Java 16 中的新功能,现在提供早期访问版本。 In a record you simply declare the member fields.在记录中,您只需声明成员字段。 The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .编译器隐式创建构造函数、getter、 equals & hashCodetoString

So no need for you to override equals wih your own implementation as seen in the other Answers.因此,您无需像其他答案中所见的那样用您自己的实现来覆盖equals

Use a record where the primary purpose of a class is to immutably and transparently carry data.使用 class 的主要目的是不可变和透明地携带数据的记录。 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. 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#equals查看两个列表是否包含相同顺序的相等元素。

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)如果您在我们的 class 中覆盖equals() ,您应该能够使用myFirstList.sort()对两个列表进行排序,然后只需使用myFirstList.equals(mySecondList)

This is not related to ArrayList, it is related to your Test Class.这与 ArrayList 无关,它与您的测试 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:正如@VGR 所说,为了知道测试 Class 的两个对象是否具有相同的字段,您必须像这样覆盖测试 class 中的 equals 方法:

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:现在检查 ArrayList 中的两个对象是否相等,只需执行以下操作:

    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.显然,要在两个 ArrayList 中搜索相等的对象,您必须遍历第一个列表中的每个 object,然后将 object 与第二个列表中的所有对象进行比较,或者实现您自己的算法。 That's where data structure and algorithms are used!这就是使用数据结构和算法的地方!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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