Collections

synchronized

ArrayList相关

Collections.synchronizedList(new ArrayList<>())

1
2
3
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
         
    }

  

 

1
2
3
4
// 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));
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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<>())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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() 

...

posted on   anpeiyong  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示