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.

3
  • 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? Commented May 31, 2013 at 5: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 Commented May 31, 2013 at 5:53
  • 1
    Probably because you have not implemented equals and hashCode. Commented May 31, 2013 at 5:54

2 Answers 2

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().

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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 the problem is that you're verifying if your list contains a List<UnassignedSubjectData> instance, which will always give you false. Please read the JavaDoc of List#contains
@Keppil where and how do i implement the two functions that you have suggested in your answer?
@7bluephoenix: You are very welcome. Maybe you didn't need to override hashCode() now, but you really should. Here is an explanation why.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.