繁体   English   中英

比较 ArrayList 与包含 object

[英]compare ArrayList with contain object

我有我的 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;
}

和此 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));

现在我需要检查这些数组列表是否包含内部具有相同字段的对象; 除了使用fori循环并获取每个参数进行比较之外,还有其他方法吗?

记录

显然,您想通过检查每个成员字段的内容来比较相同 class 的对象是否相等。

您可以通过使用记录功能自动获得该行为。 此功能是 Java 16 中的新功能,现在提供早期访问版本。 在记录中,您只需声明成员字段。 编译器隐式创建构造函数、getter、 equals & hashCodetoString

因此,您无需像其他答案中所见的那样用您自己的实现来覆盖equals

使用 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.

所以你的代码:

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;
}

……变成:

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

使用该记录:

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

比较列表

使用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. 

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

这与 ArrayList 无关,它与您的测试 Class 有关。 正如@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; 
    } 
}

现在检查 ArrayList 中的两个对象是否相等,只需执行以下操作:

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

显然,要在两个 ArrayList 中搜索相等的对象,您必须遍历第一个列表中的每个 object,然后将 object 与第二个列表中的所有对象进行比较,或者实现您自己的算法。 这就是使用数据结构和算法的地方!

暂无
暂无

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

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