简体   繁体   English

检查一个 ArrayList 是否包含与另一个 ArrayList 相同的元素

[英]Check if an ArrayList contains the same element as another ArrayList

I have used some other answers to get a solution to my problem.我使用了其他一些答案来解决我的问题。 But I am wondering if there is a way to improve this further?但我想知道是否有办法进一步改善这一点?

// Copy the masterList ArrayList and then sort in ascending order and then make a third 
// ArrayList and loop through to add the 8 lowest values to this list.
ArrayList<Integer> sortedList = new ArrayList<>(Calculator.masterList);
Collections.sort(sortedList);
ArrayList<Integer> lowEight = new ArrayList<>();
for (int i = 0; i < 8; i++) {
    lowEight.add(sortedList.get(i));
}
// Set TextView as the value of index 0 in masterList ArrayList, check if lowEight 
// ArrayList contains the element that is the same as masterList index 0 and if
// so highlight s1 textview green.
s1.setText("Score 1 is " + String.format("%d", Calculator.masterList.get(0)));
if (lowEight.contains(Calculator.masterList.get(0))) {
    s1.setBackgroundColor(Color.GREEN);
}

This works to an extent by highlighting the values that are in both masterList and lowEight but for example if the number 7 is in lowEight and appears 9 times in masterList it will highlight all 9 occurences.这通过突出显示是在两个值工作的程度masterListlowEight但例如如果数字7是lowEight并显示在9倍masterList它将突出显示所有9个OCCURENCES。 Is there a way to move the exact object from masterList to sortedList and then to lowEight and then a method to check the object and not just the value?有没有办法将确切的对象从masterList移动到sortedList ,然后移动到lowEight ,然后是一种检查对象而不仅仅是值的方法?

Let me provide a more concise example of what you're asking.让我提供一个更简洁的示例来说明您的要求。 Let us take the following code:让我们使用以下代码:

ArrayList<Integer> list1 = new ArrayList<Integer>() {
    {
        add(5);
        add(5);
    }
};

ArrayList<Integer> list2 = new ArrayList<>();
list2.add(list1.get(0));
list1.forEach((i) -> System.out.println(list2.contains(i)));

The output is:输出是:

true
true

But you would expect it to be:但你会期望它是:

true
false

Because the first and second element are different objects.因为第一个和第二个元素是不同的对象。 The problem here is that although they are different objects, they are equal objects.这里的问题是,虽然它们是不同的对象,但它们是相同的对象。 The way the Integer class is written in Java, any Integer is equal to another Integer if they represent the same value. Integer 类是用 Java 编写的,如果它们表示相同的值,则任何 Integer 都等于另一个 Integer。 When you run the contains() method, it sees that the list does indeed contain an object equal to the one you provided (in this case they both represent a value of 5), and so it returns true.当您运行contains()方法时,它会看到该列表确实包含一个与您提供的对象相同的对象(在这种情况下,它们都表示值 5),因此它返回 true。 So how do we solve this problem?那么我们如何解决这个问题呢? How do we tell one Integer object from another?我们如何区分一个 Integer 对象和另一个? I would write your own "Integer" class.我会写你自己的“整数”类。 Something like "MyInteger".像“MyInteger”这样的东西。 Here's a very simple implementation you could use:这是您可以使用的一个非常简单的实现:

public class MyInteger {
    private final int i;

    public MyInteger(int i) {
        this.i = i;
    }

    public int toInt() {
        return i;
    }
}

And then when we use it in our ArrayList code:然后当我们在 ArrayList 代码中使用它时:

ArrayList<MyInteger> list1 = new ArrayList<MyInteger>() {
    {
        add(new MyInteger(5));
        add(new MyInteger(5));
    }
};

ArrayList<MyInteger> list2 = new ArrayList<>();
list2.add(list1.get(0));
list1.forEach((i) -> System.out.println(list2.contains(i)));

We get our expected output:我们得到我们的预期输出:

true
false

This works because our new MyInteger class implicitly uses the default equals() method, which always returns false.这是有效的,因为我们的新 MyInteger 类隐式使用默认的equals()方法,该方法总是返回 false。 In other words, no two MyInteger objects are ever equal.换句话说,没有两个 MyInteger 对象永远相等。 You can apply this same principle to your code.您可以将同样的原则应用到您的代码中。

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

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