Nuñito Calzada
Nuñito Calzada

Reputation: 2066

Java UnsupportedOperationException with Collection objects

I have an error using Java Collections in JDK 1.7 : I got this Exception in this line: proposalStatuses.addAll(getAllSubmittedStatuses())

java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(Unknown Source)
        at java.util.AbstractList.add(Unknown Source)
        at java.util.AbstractCollection.addAll(Unknown Source)

trying to add a collection to a list

/**
     * Gets the all submitted statuses.
     *
     * @return the all submitted statuses
     */
    private Collection<ProposalStatus> getAllSubmittedStatuses() {

        return Arrays.asList(
                  ProposalStatus.SAVED_TO_IOS
                , ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
                );
    }

    /**
     * Gets the all received statuses.
     *
     * @return the all received statuses
     */
    private Collection<ProposalStatus> getAllReceivedStatuses() {

        Collection<ProposalStatus> proposalStatuses =

                Arrays.asList(
                  ProposalStatus.RECEIVED_BY_IOS
                , ProposalStatus.SUBMITTED_TO_IOS
                , ProposalStatus.RECEIVED_IOS
                );

        proposalStatuses.addAll(getAllSubmittedStatuses());

        return proposalStatuses;
    }

Upvotes: 5

Views: 1733

Answers (3)

ha9u63a7
ha9u63a7

Reputation: 6824

To explain a bit more practically, I am using your code:

private Collection<ProposalStatus> getAllSubmittedStatuses() {

    // This returns a list that cannot be modified, fixed size
    return Arrays.asList(
              ProposalStatus.SAVED_TO_IOS
            , ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
            );
}

/**
 * Gets the all received statuses.
 *
 * @return the all received statuses
 */
private Collection<ProposalStatus> getAllReceivedStatuses() {

    // proposalStatuses will be a fixed-size list so no changing
    Collection<ProposalStatus> proposalStatuses =

            Arrays.asList(
              ProposalStatus.RECEIVED_BY_IOS
            , ProposalStatus.SUBMITTED_TO_IOS
            , ProposalStatus.RECEIVED_IOS
            );

    // This will not be possible, also you cannot remove anything.
    proposalStatuses.addAll(getAllSubmittedStatuses());

    return proposalStatuses;
}

For your purpose, I would do the the following:

return new ArrayList<ProposalStatus>(Arrays.asList(ProposalStatus.SAVED_TO_IOS,ProposalStatus.SENDED_TO_IOS_IN_PROGRESS)

This should help you get the Collection objects.

Upvotes: 1

fge
fge

Reputation: 121712

From the javadoc of Arrays.asList() (emphasis mine):

Returns a fixed-size list backed by the specified array

In short: you cannot .add*() or .remove*() from such a List! You'll have to use another modifiable List implementation (ArrayList for instance).

Upvotes: 11

jn1kk
jn1kk

Reputation: 5102

Arrays.asList() returns an immutable list, that you cannot modify.

More specifically, add(), addAll() and remove() methods are not implemented.

Upvotes: 1

Related Questions