Get a synchronized collection

static<T> Collection<T> synchronizedCollection(Collection<T> c)
Returns a synchronized (thread-safe) collection backed by the specified collection.
static<T> List<T> synchronizedList(List<T> list)
Returns a synchronized (thread-safe) list backed by the specified list.
static<K,V> Map<K,V> synchronizedMap(Map<K,V> m)
Returns a synchronized (thread-safe) map backed by the specified map.
static<T> Set<T> synchronizedSet(Set<T> s)
Returns a synchronized (thread-safe) set backed by the specified set.
static<K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.
static<T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

  import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
    List<String> list = Collections.synchronizedList(new ArrayList<String>());
    Set<String> s = Collections.synchronizedSet(new HashSet<String>());
    Map<String,String> m = Collections.synchronizedMap(new HashMap<String,String>());
  }
}
  

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MainClass {
  public static void main(String args[]) {
    Set simpsons = new HashSet();
    simpsons.add("B");
    simpsons.add("H");
    simpsons.add("L");
    simpsons = Collections.synchronizedSet(simpsons);
    synchronized (simpsons) {
      Iterator iter = simpsons.iterator();
      while (iter.hasNext()) {
        System.out.println(iter.next());
      }
    }
    Map map = Collections.synchronizedMap(new HashMap(89));
    Set set = map.entrySet();
    synchronized (map) {
      Iterator iter = set.iterator();
      while (iter.hasNext()) {
        System.out.println(iter.next());
      }
    }
  }
}

The output:


H
B
L
Home 
  Java Book 
    Collection  

Collections:
  1. Collections
  2. Get empty collection from Collections
  3. Do binary search
  4. Copy value to a List
  5. Get Enumeration from collection, create list from Enumeration
  6. Fill a list
  7. Get the max and min value from a Collection
  8. Fill n Copy object to a list
  9. Replace value in a list
  10. Reverse a list
  11. Get comparator in reverse order
  12. Rotate and shuffle a list
  13. Create singleton
  14. Sort a list
  15. Swap element in a list
  16. Get a synchronized collection
  17. Return an unmodifiable view of collections