Unmodifiable List in java

I'm trying to set a List unmodifiable.

In my code, i have a method which returns a list.

This list should'nt be modified, but i dont want to catch the exception returned by the unmodifiableList.

private List<T> listeReferenceSelectAll = null;
List<T> oListeRet = new ArrayList<T>();
oListeRet = listeReferenceSelectAll;
return new ArrayList<T>(oListeRet);

It is an existing code and i have to transform it to return an unmodifiable list, but if an "add" method has been called, no exception has to be caught.

First i have create a class which implements List to override "add" method to log the exception and not to catch it.

But i don't know how corretly instanciate it...

If you have any other solutions, Thanks in advance for your help :).

Answers


If you absolutely must do this, try to follow the contract specified by java.util.List in the list you are creating.

Your code would look something like

public class UnmodifiableArrayList<E>  extends ArrayList<E> {

    public UnmodifiableArrayList(Collection<? extends E> c) {
        super(c);
    }

    public boolean add(int index) {
        return false;//Returning false as the element cannot be added 
    }

    public boolean addAll(Collection<? extends E> c) {
        return false;//Returning false as the element cannot be added 
    }

    public E remove(int index) {
        return null;//Returning null as the element cannot be removed
    }
}

Add any more methods you need on the same lines. Just ensure that all the constructors and methods that might by used to modify in your code are overriden, so as to ensure the list is unmodifiable.

Using the Collections API is the cleaner and better way to do it, so use this way only if using Collections.UnmodifiableList does not satisfy your need.

And keep in mind this will be a nightmare while debugging so log as much as possible.


You need java.util.Collections:

return Collections.unmodifiableList(oListeRet);

If you have to write your own, have that class implement the List interface and throw exceptions for the methods that modify contents.


Collections.unmodifiableList

Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException. The returned list will be serializable if the specified list is serializable. Similarly, the returned list will implement RandomAccess if the specified list does.


Need Your Help

Guice ServeContextListener does not kick-in

java guice

I'm trying to do a @Named inject for a bound String in the ServeContextListener, however I always get that the String is not bound exception. So I guess the ServeContextListener does not kick in.

Problem with Eclipse while programming for android - “Refreshing external folders”

android eclipse refresh save

I have set up Eclipse to run as smoothly as possible because I was having a lot of problems with it running out of memory in my last machine. One of the configurations was to have it automatically