Collections
synchronized
ArrayList相关
Collections.synchronizedList(new ArrayList<>())
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ }
// java.util.Collections.synchronizedList(java.util.List<T>) public static <T> List<T> synchronizedList(List<T> list) { return (list instanceof RandomAccess ? new SynchronizedRandomAccessList<>(list) : new SynchronizedList<>(list)); }
public class Collections { static class SynchronizedCollection<E> implements Collection<E>, Serializable { final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize SynchronizedCollection(Collection<E> c) { this.c = Objects.requireNonNull(c); mutex = this; } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { synchronized (mutex) {return c.add(e);} } public boolean remove(Object o) { synchronized (mutex) {return c.remove(o);} } } static class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> { final List<E> list; SynchronizedList(List<E> list) { super(list); this.list = list; } public E get(int index) { synchronized (mutex) {return list.get(index);} } public E set(int index, E element) { synchronized (mutex) {return list.set(index, element);} } public void add(int index, E element) { synchronized (mutex) {list.add(index, element);} } public E remove(int index) { synchronized (mutex) {return list.remove(index);} } } static class SynchronizedRandomAccessList<E> extends SynchronizedList<E> implements RandomAccess { SynchronizedRandomAccessList(List<E> list) { super(list); } } }
LinkedList相关
Collections.synchronizedList(new LinkedList<>())
public static <T> List<T> synchronizedList(List<T> list) { return (list instanceof RandomAccess ? new SynchronizedRandomAccessList<>(list) : new SynchronizedList<>(list)); } public class Collections { static class SynchronizedCollection<E> implements Collection<E>, Serializable { final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize SynchronizedCollection(Collection<E> c) { this.c = Objects.requireNonNull(c); mutex = this; } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { synchronized (mutex) {return c.add(e);} } public boolean remove(Object o) { synchronized (mutex) {return c.remove(o);} } } static class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> { final List<E> list; SynchronizedList(List<E> list) { super(list); this.list = list; } public E get(int index) { synchronized (mutex) {return list.get(index);} } public E set(int index, E element) { synchronized (mutex) {return list.set(index, element);} } public void add(int index, E element) { synchronized (mutex) {list.add(index, element);} } public E remove(int index) { synchronized (mutex) {return list.remove(index);} } } }
HashMap/LinkedHashMap相关
Collections.synchronizedMap(new HashMap<String, Object>()/new LinkedHashMap<>())
// java.util.Collections.synchronizedMap public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) { return new SynchronizedMap<>(m); } public class Collections { private static class SynchronizedMap<K,V> implements Map<K,V>, Serializable { private final Map<K,V> m; // Backing Map final Object mutex; // Object on which to synchronize private transient Set<K> keySet; private transient Set<Map.Entry<K,V>> entrySet; private transient Collection<V> values; SynchronizedMap(Map<K,V> m) { this.m = Objects.requireNonNull(m); mutex = this; } public V get(Object key) { synchronized (mutex) {return m.get(key);} } public V put(K key, V value) { synchronized (mutex) {return m.put(key, value);} } public V remove(Object key) { synchronized (mutex) {return m.remove(key);} } public Set<K> keySet() { synchronized (mutex) { if (keySet==null) keySet = new SynchronizedSet<>(m.keySet(), mutex); return keySet; } } public Set<Map.Entry<K,V>> entrySet() { synchronized (mutex) { if (entrySet==null) entrySet = new SynchronizedSet<>(m.entrySet(), mutex); return entrySet; } } public Collection<V> values() { synchronized (mutex) { if (values==null) values = new SynchronizedCollection<>(m.values(), mutex); return values; } } } }
TreeMap相关
Collections.synchronizedSortedMap(new TreeMap<String, String>())
// java.util.Collections.synchronizedSortedMap public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) { return new SynchronizedSortedMap<>(m); } public class Collections { private static class SynchronizedMap<K,V> implements Map<K,V>, Serializable { private final Map<K,V> m; // Backing Map final Object mutex; // Object on which to synchronize private transient Set<K> keySet; private transient Set<Map.Entry<K,V>> entrySet; private transient Collection<V> values; SynchronizedMap(Map<K,V> m) { this.m = Objects.requireNonNull(m); mutex = this; } public V get(Object key) { synchronized (mutex) {return m.get(key);} } public V put(K key, V value) { synchronized (mutex) {return m.put(key, value);} } public V remove(Object key) { synchronized (mutex) {return m.remove(key);} } public Set<K> keySet() { synchronized (mutex) { if (keySet==null) keySet = new SynchronizedSet<>(m.keySet(), mutex); return keySet; } } public Set<Map.Entry<K,V>> entrySet() { synchronized (mutex) { if (entrySet==null) entrySet = new SynchronizedSet<>(m.entrySet(), mutex); return entrySet; } } public Collection<V> values() { synchronized (mutex) { if (values==null) values = new SynchronizedCollection<>(m.values(), mutex); return values; } } } static class SynchronizedSortedMap<K,V> extends SynchronizedMap<K,V> implements SortedMap<K,V>{ private final SortedMap<K,V> sm; SynchronizedSortedMap(SortedMap<K,V> m) { super(m); sm = m; } } }
HashSet/LinkedHashSet相关
Collections.synchronizedSet(new HashSet<>()/new LinkedHashSet<>())
// java.util.Collections.synchronizedSet(java.util.Set<T>) public static <T> Set<T> synchronizedSet(Set<T> s) { return new SynchronizedSet<>(s); } public class Collections { static class SynchronizedCollection<E> implements Collection<E>, Serializable { final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize SynchronizedCollection(Collection<E> c) { this.c = Objects.requireNonNull(c); mutex = this; } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { synchronized (mutex) {return c.add(e);} } public boolean remove(Object o) { synchronized (mutex) {return c.remove(o);} } } static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> { SynchronizedSet(Set<E> s) { super(s); } } }
TreeSet相关
Collections.synchronizedSortedSet(new TreeSet<>())
// java.util.Collections.synchronizedSortedSet public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) { return new SynchronizedSortedSet<>(s); } public class Collections { static class SynchronizedCollection<E> implements Collection<E>, Serializable { final Collection<E> c; // Backing Collection final Object mutex; // Object on which to synchronize SynchronizedCollection(Collection<E> c) { this.c = Objects.requireNonNull(c); mutex = this; } public Iterator<E> iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(E e) { synchronized (mutex) {return c.add(e);} } public boolean remove(Object o) { synchronized (mutex) {return c.remove(o);} } } static class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> { SynchronizedSet(Set<E> s) { super(s); } } static class SynchronizedSortedSet<E> extends SynchronizedSet<E> implements SortedSet<E>{ private final SortedSet<E> ss; SynchronizedSortedSet(SortedSet<E> s) { super(s); ss = s; } } }
unmodifiable
Collections.unmodifiableList(new ArrayList<>()/new LinkedList<>())
// java.util.Collections.unmodifiableList public static <T> List<T> unmodifiableList(List<? extends T> list) { return (list instanceof RandomAccess ? new UnmodifiableRandomAccessList<>(list) : new UnmodifiableList<>(list)); } public class Collections { static class UnmodifiableCollection<E> implements Collection<E>, Serializable { final Collection<? extends E> c; UnmodifiableCollection(Collection<? extends E> c) { if (c==null) throw new NullPointerException(); this.c = c; } public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); } public boolean removeAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public boolean retainAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } } static class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> { final List<? extends E> list; UnmodifiableList(List<? extends E> list) { super(list); this.list = list; } public E set(int index, E element) { throw new UnsupportedOperationException(); } public void add(int index, E element) { throw new UnsupportedOperationException(); } public E remove(int index) { throw new UnsupportedOperationException(); } } static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E> implements RandomAccess { UnmodifiableRandomAccessList(List<? extends E> list) { super(list); } } }
Collections.unmodifiableSet(new HashSet<>()/new LinkedHashSet<>())
// java.util.Collections.unmodifiableSet public static <T> Set<T> unmodifiableSet(Set<? extends T> s) { return new UnmodifiableSet<>(s); } public class Collections { static class UnmodifiableCollection<E> implements Collection<E>, Serializable { final Collection<? extends E> c; UnmodifiableCollection(Collection<? extends E> c) { if (c==null) throw new NullPointerException(); this.c = c; } public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); } public boolean removeAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public boolean retainAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } } static class UnmodifiableSet<E> extends UnmodifiableCollection<E> implements Set<E>, Serializable { UnmodifiableSet(Set<? extends E> s) {super(s);} } }
Collections.unmodifiableSortedSet(new TreeSet<>())
// java.util.Collections.unmodifiableSortedSet public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) { return new UnmodifiableSortedSet<>(s); } public class Collections { static class UnmodifiableCollection<E> implements Collection<E>, Serializable { final Collection<? extends E> c; UnmodifiableCollection(Collection<? extends E> c) { if (c==null) throw new NullPointerException(); this.c = c; } public boolean addAll(Collection<? extends E> coll) { throw new UnsupportedOperationException(); } public boolean removeAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public boolean retainAll(Collection<?> coll) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } } static class UnmodifiableSet<E> extends UnmodifiableCollection<E> implements Set<E>, Serializable { UnmodifiableSet(Set<? extends E> s) {super(s);} } static class UnmodifiableSortedSet<E> extends UnmodifiableSet<E> implements SortedSet<E>, Serializable { private final SortedSet<E> ss; UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;} } }
Collections.unmodifiableMap(new HashMap<>()/new LinkedHashMap<>())
// java.util.Collections.unmodifiableMap public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) { return new UnmodifiableMap<>(m); } public class Collections { private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable { private final Map<? extends K, ? extends V> m; UnmodifiableMap(Map<? extends K, ? extends V> m) { if (m==null) throw new NullPointerException(); this.m = m; } public V put(K key, V value) { throw new UnsupportedOperationException(); } public V remove(Object key) { throw new UnsupportedOperationException(); } public void putAll(Map<? extends K, ? extends V> m) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } } }
Collections.unmodifiableSortedMap(new TreeMap<>())
// java.util.Collections.unmodifiableSortedMap public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) { return new UnmodifiableSortedMap<>(m); } public class Collections { private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable { private final Map<? extends K, ? extends V> m; UnmodifiableMap(Map<? extends K, ? extends V> m) { if (m==null) throw new NullPointerException(); this.m = m; } public V put(K key, V value) { throw new UnsupportedOperationException(); } public V remove(Object key) { throw new UnsupportedOperationException(); } public void putAll(Map<? extends K, ? extends V> m) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } } static class UnmodifiableSortedMap<K,V> extends UnmodifiableMap<K,V> implements SortedMap<K,V>, Serializable { private final SortedMap<K, ? extends V> sm; UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; } } }
empty
Collections.emptyList()
// java.util.Collections.emptyList public static final <T> List<T> emptyList() { return (List<T>) EMPTY_LIST; } public class Collections { public static final List EMPTY_LIST = new EmptyList<>(); private static class EmptyList<E> extends AbstractList<E> implements RandomAccess, Serializable { public int size() {return 0;} public boolean isEmpty() {return true;} public boolean contains(Object obj) {return false;} public boolean containsAll(Collection<?> c) { return c.isEmpty(); } public Object[] toArray() { return new Object[0]; } public E get(int index) { throw new IndexOutOfBoundsException("Index: "+index); } } }
Collections.emptySet()
// java.util.Collections.emptySet public static final <T> Set<T> emptySet() { return (Set<T>) EMPTY_SET; } public class Collections { public static final Set EMPTY_SET = new EmptySet<>(); private static class EmptySet<E> extends AbstractSet<E> implements Serializable{ public int size() {return 0;} public boolean isEmpty() {return true;} public boolean contains(Object obj) {return false;} public boolean containsAll(Collection<?> c) { return c.isEmpty(); } } }
Collections.emptySortedSet()、Collections.emptyMap()、Collections.emptySortedMap()
...