I try to add objects to a List<String>
instance but it throws an UnsupportedOperationException
.
Does anyone know why?
My Java code:
String[] membersArray = request.getParameterValues('members');
List<String> membersList = Arrays.asList(membersArray);
for (String member : membersList) {
Person person = Dao.findByName(member);
List<String> seeAlso;
seeAlso = person.getSeeAlso();
if (!seeAlso.contains(groupDn)){
seeAlso.add(groupDn);
person.setSeeAlso(seeAlso);
}
}
The error message:
java.lang.UnsupportedOperationException java.util.AbstractList.add(Unknown Source) java.util.AbstractList.add(Unknown Source) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Form the Inheritance concept, If some perticular method is not available in the current class it will search for that method in super classes. If available it executes.
When you are converting from an Array to a Collection Obejct. i.e., array-based to collection-based API then it is going to provide you fixed-size collection object, because Array's behaviour is of Fixed size.
Souce samples for conformation.
Form the above Source you may observe that
java.util.Arrays.ArrayList
class doesn't@Override add(index, element), set(index, element), remove(index)
. So, From inheritance it executes superAbstractList<E>
classadd()
function which throwsUnsupportedOperationException
.As
AbstractList<E>
is an abstract class it provides the implementation toiterator() and listIterator()
. So, that we can iterate over the list object.You can even create Fixed-Size array form Collections class
Collections.unmodifiableList(list);
Sample Source:
A
Collection
— sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.@see also
vs
HashTableWith the minimal change in your code, you can do below to convert a list to ArrayList. The first solution is having a minimum change in your solution, but the second one is more optimized, I guess.
OR
You cannot modify a result from a LDAP query. Your problem is in this line:
The seeAlso list is unmodifiable.
List membersList = Arrays.asList(membersArray);
returns immutable list, what you need to do is
new ArrayList<>(Arrays.asList(membersArray)); to make it mutable
Not every
List
implementation supports theadd()
method.One common example is the
List
returned byArrays.asList()
: it is documented not to support any structural modification (i.e. removing or adding elements) (emphasis mine):Even if that's not the specific
List
you're trying to modify, the answer still applies to otherList
implementations that are either immutable or only allow some selected changes.You can find out about this by reading the documentation of
UnsupportedOperationException
andList.add()
, which documents this to be an "(optional operation)". The precise meaning of this phrase is explained at the top of theList
documentation.As a workaround you can create a copy of the list to a known-modifiable implementation like
ArrayList
:You must initialize your List seeAlso :
or