1

I am using the following code:

final UnassignedSubjectData selsub = (UnassignedSubjectData) spinSelectSubject
            .getSelectedItem();
ArrayList<UnassignedSubjectData> selectedSubjectList = null;
    if (selsubdata != null) {
        selectedSubjectList = new ArrayList<UnassignedSubjectData>(
                Arrays.asList(selsubdata));
        Log.d(LOGTAG, "Check " + selsub.toString());
        Log.d(LOGTAG, "Check " + selectedSubjectList.toString());
        Log.d(LOGTAG, "Result for if "
                + Arrays.asList(selsubdata).contains(Arrays.asList(selsub)));
if (selectedSubjectList.contains(Arrays.asList(selsub))) {
            CustomToast.showCustomToast(this,
                    "Subject already present in list");
            Log.d(LOGTAG,
                    "IN IF after TOAST " + selectedSubjectList.toString());
            return;
        }
        else
            Log.d(LOGTAG, "Showing subject not in list");
    }

The selsub is an object of UnassignedSubjectData.

I get the following in the for one of the conditions in LogCat:

Check History
Check [History, Science, Science, History]
Result for if false
Showing subject not in list

That means even when the object is present in the ArrayList the .contains() operator is not working properly. Please help me find a solution for this.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
7bluephoenix
  • 958
  • 7
  • 18
  • 1
    Have you implemented `equals` and `hashCode` for your `UnassignedSubjectData` class? And why are you putting a list of one element inside the other list? – NilsH May 31 '13 at 05:49
  • @NilsH i put one element inside other list, just so because the .contains() with one element was not working... however trying this also didnt work – 7bluephoenix May 31 '13 at 05:53
  • 1
    Probably because you have not implemented `equals` and `hashCode`. – NilsH May 31 '13 at 05:54

2 Answers2

3

First, as @LuiggiMendoza points out, the call

Arrays.asList(selsubdata).contains(Arrays.asList(selsub))

will never return true here, since you want to look for an element, not a sublist. Change this to:

Arrays.asList(selsubdata).contains(selsub)

Second, List#contains() uses the equals() method of the list elements for comparison. The default equals() inherited from Object compares references, which will not work as you want.

For contains() to work properly for your object, you need to implement equals() (and hashCode()) for UnassignedSubjectData.

hashCode() is not actually needed for comparison, but it should always be implemented together with equals().

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 2
    If you read the code carefully, OP's trying to verify if `Arrays.asList(selsubdata)` contains `Arrays.asList(selsub)`. After fixing this, your answer is valid. – Luiggi Mendoza May 31 '13 at 05:50
  • @LuiggiMendoza ya u r right... i put one element inside other list with asList, just so because the .contains() with one element was not working... however trying this also didnt work – 7bluephoenix May 31 '13 at 05:55
  • 1
    @7bluephoenix the problem is that you're verifying if your list `contains` a `List` instance, which will always give you `false`. Please read the JavaDoc of [`List#contains`](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains(java.lang.Object)) – Luiggi Mendoza May 31 '13 at 05:57
  • @Keppil where and how do i implement the two functions that you have suggested in your answer? – 7bluephoenix May 31 '13 at 05:58
  • @Keppil & LuiggiMendoza .... thanks for the help... it worked... actually i did not even have to change the overridden hashCode() function.... – 7bluephoenix May 31 '13 at 06:30
  • 1
    @7bluephoenix: You are very welcome. Maybe you didn't need to override `hashCode()` now, but you really should. [Here](http://stackoverflow.com/a/2707554/1343161) is an explanation why. – Keppil May 31 '13 at 07:26
1

Ovevride equals() and hashcode() for the properties on the basis of which you want to define equality. Then, contains() will work for those properties.

Sumit Desai
  • 1,542
  • 9
  • 22