Class SynchronizedCollection<E>

  • Type Parameters:
    E - the type of the elements in the collection
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>
    Direct Known Subclasses:
    SynchronizedBag, SynchronizedMultiSet, SynchronizedQueue

    public class SynchronizedCollection<E>
    extends java.lang.Object
    implements java.util.Collection<E>, java.io.Serializable
    Decorates another Collection to synchronize its behaviour for a multi-threaded environment.

    Iterators must be manually synchronized:

     synchronized (coll) {
       Iterator it = coll.iterator();
       // do stuff with iterator
     }
     

    This class is Serializable from Commons Collections 3.1.

    Since:
    3.0
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E object)  
      boolean addAll​(java.util.Collection<? extends E> coll)  
      void clear()  
      boolean contains​(java.lang.Object object)  
      boolean containsAll​(java.util.Collection<?> coll)  
      boolean equals​(java.lang.Object object)  
      int hashCode()  
      boolean isEmpty()  
      java.util.Iterator<E> iterator()
      Iterators must be manually synchronized.
      boolean remove​(java.lang.Object object)  
      boolean removeAll​(java.util.Collection<?> coll)  
      boolean removeIf​(java.util.function.Predicate<? super E> filter)  
      boolean retainAll​(java.util.Collection<?> coll)  
      int size()  
      static <T> SynchronizedCollection<T> synchronizedCollection​(java.util.Collection<T> coll)
      Factory method to create a synchronized collection.
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] object)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

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

        forEach
    • Method Detail

      • synchronizedCollection

        public static <T> SynchronizedCollection<T> synchronizedCollection​(java.util.Collection<T> coll)
        Factory method to create a synchronized collection.
        Type Parameters:
        T - the type of the elements in the collection
        Parameters:
        coll - the collection to decorate, must not be null
        Returns:
        a new synchronized collection
        Throws:
        java.lang.NullPointerException - if collection is null
        Since:
        4.0
      • add

        public boolean add​(E object)
        Specified by:
        add in interface java.util.Collection<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.Collection<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
      • contains

        public boolean contains​(java.lang.Object object)
        Specified by:
        contains in interface java.util.Collection<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> coll)
        Specified by:
        containsAll in interface java.util.Collection<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Iterators must be manually synchronized.
         synchronized (coll) {
           Iterator it = coll.iterator();
           // do stuff with iterator
         }
         
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Returns:
        an iterator that must be manually synchronized on the collection
      • toArray

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

        public <T> T[] toArray​(T[] object)
        Specified by:
        toArray in interface java.util.Collection<E>
      • remove

        public boolean remove​(java.lang.Object object)
        Specified by:
        remove in interface java.util.Collection<E>
      • removeIf

        public boolean removeIf​(java.util.function.Predicate<? super E> filter)
        Specified by:
        removeIf in interface java.util.Collection<E>
        Since:
        4.4
      • removeAll

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

        public boolean retainAll​(java.util.Collection<?> coll)
        Specified by:
        retainAll in interface java.util.Collection<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
      • equals

        public boolean equals​(java.lang.Object object)
        Specified by:
        equals in interface java.util.Collection<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object