Class UnmodifiableArrayList<T>

  • Type Parameters:
    T - element type of the underlying array
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>

    @NotThreadSafe
    public class UnmodifiableArrayList<T>
    extends java.lang.Object
    implements java.util.List<T>
    This class provides an unmodifiable List proxy for an underlying array. In other words, this helps you use the array like a List, but not being able to modify it. Create an instance with the constructor and get the underlying array with toArray(). The difference to Collections.unmodifiableList(Arrays.asList(array)) is, in some Java implementations like adoptjdk, Arrays.asList() copies the array with new ArrayList<>(array) so it is not performant. Also this class makes it possible to recover the original array from the List, list.toArray() returns the underlying array instead of a copy. Use the underlying array for modification if needed. Use this when you just want to return an unmodifiable view of the array. There is no extra thread safety guarantee on this List. When the underlying array changes, the view will change too, and there is no extra thread safety.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, T element)  
      boolean add​(T t)  
      boolean addAll​(int index, java.util.Collection<? extends T> c)  
      boolean addAll​(java.util.Collection<? extends T> c)  
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      T get​(int index)  
      int indexOf​(java.lang.Object o)  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      int lastIndexOf​(java.lang.Object o)  
      java.util.ListIterator<T> listIterator()  
      java.util.ListIterator<T> listIterator​(int index)  
      T remove​(int index)  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      T set​(int index, T element)  
      int size()  
      java.util.List<T> subList​(int fromIndex, int toIndex)  
      java.lang.Object[] toArray()  
      <T1> T1[] toArray​(T1[] a)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        equals, hashCode, replaceAll, sort, spliterator
    • Constructor Detail

      • UnmodifiableArrayList

        public UnmodifiableArrayList​(T[] elements)
        Parameters:
        elements - the underlying array
    • Method Detail

      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.List<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.List<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.List<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
      • toArray

        public <T1> T1[] toArray​(T1[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
      • add

        public boolean add​(T t)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
      • add

        public void add​(int index,
                        T element)
        Specified by:
        add in interface java.util.List<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.List<T>
      • remove

        public T remove​(int index)
        Specified by:
        remove in interface java.util.List<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Specified by:
        containsAll in interface java.util.List<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.Collection<T>
        Specified by:
        addAll in interface java.util.List<T>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.List<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
        Specified by:
        removeAll in interface java.util.List<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
        Specified by:
        retainAll in interface java.util.List<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.List<T>
      • get

        public T get​(int index)
        Specified by:
        get in interface java.util.List<T>
      • set

        public T set​(int index,
                     T element)
        Specified by:
        set in interface java.util.List<T>
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<T>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator()
        Specified by:
        listIterator in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<T>
      • subList

        public java.util.List<T> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<T>