Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
like image 921
Nunyet de Can Calçada Avatar asked Jan 21 '15 14:01

Nunyet de Can Calçada


People also ask

How do I resolve Java Lang UnsupportedOperationException?

The UnsupportedOperationException can be resolved by using a mutable collection, such as ArrayList , which can be modified. An unmodifiable collection or data structure should not be attempted to be modified.

What is List of () in Java?

The Java. util. List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

How do you add to a List in Java?

There are two methods to add elements to the list. add(E e ) : appends the element at the end of the list. Since List supports Generics , the type of elements that can be added is determined when the list is created. add(int index , E element ) : inserts the element at the given index .

Which of the following operation's are not supported on the List created above using the asList () method?

asList returns a fixed size List backed by an array. Therefore remove and add are not supported.


2 Answers

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

like image 139
fge Avatar answered Oct 06 '22 00:10

fge


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

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

like image 28
jn1kk Avatar answered Oct 05 '22 22:10

jn1kk