简体   繁体   中英

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. 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?

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. 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. So how do we solve this problem? How do we tell one Integer object from another? I would write your own "Integer" class. Something like "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<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. In other words, no two MyInteger objects are ever equal. You can apply this same principle to your code.

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