java.util.Collections

2018-10-07  本文已影响26人  游牧族人

Collections 内部类

static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
        private static final long serialVersionUID = 1820017752578914078L;

        final Collection<? extends E> c;  

        UnmodifiableCollection(Collection<? extends E> c) {
            if (c==null)
                throw new NullPointerException();
            this.c = c;
        }
        public boolean add(E e) {
            throw new UnsupportedOperationException();
        }
        ......// 各种集合操作方法  
            // 其中修改集合的方法都会抛出 UnsupportedOperationException 异常。
 }
-------------------------------------------------------------------------------------
UnmodifiableCollection
  -- UnmodifiableList
  -- UnmodifiableRandomAccessList
  -- UnmodifiableSet
  -- UnmodifiableSortedSet
  -- UnmodifiableNavigableSet
  -- UnmodifiableMap
  -- UnmodifiableSortedMap
  -- UnmodifiableNavigableMap
    static class SynchronizedCollection<E> implements Collection<E>, Serializable {
        private static final long serialVersionUID = 3053995032091335093L;

        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;
        }

        SynchronizedCollection(Collection<E> c, Object mutex) {
            this.c = Objects.requireNonNull(c);
            this.mutex = Objects.requireNonNull(mutex);
        }
        public boolean add(E e) {
            synchronized (mutex) {return c.add(e);}
        }
       ...... // 各种集合操作方法
            // 其中所有方法均使用了 synchroized 关键字进行了同步处理。
 }
-------------------------------------------------------------------------------------
SynchronizedCollection
  -- SynchronizedList
  -- SynchronizedRandomAccessList
  -- SynchronizedSet
  -- SynchronizedSortedSet
  -- SynchronizedNavigableSet
  -- SynchronizedMap
  -- SynchronizedSortedMap
  -- SynchronizedNavigableMap
static class CheckedCollection<E> implements Collection<E>, Serializable {
        private static final long serialVersionUID = 1578914078182001775L;

        final Collection<E> c;
        final Class<E> type;

        @SuppressWarnings("unchecked")
        E typeCheck(Object o) {
            if (o != null && !type.isInstance(o))
                throw new ClassCastException(badElementMsg(o));
            return (E) o;
        }

        private String badElementMsg(Object o) {
            return "Attempt to insert " + o.getClass() +
                " element into collection with element type " + type;
        }

        CheckedCollection(Collection<E> c, Class<E> type) {
            this.c = Objects.requireNonNull(c, "c");
            this.type = Objects.requireNonNull(type, "type");
        }
        public boolean add(E e){     
            return c.add(typeCheck(e)); 
        }
        ...... // 各种集合操作方法
   // add 方法会先检查传入值的类型是否与当前集合允许类型相符,不符则抛出异常。其他方法正常。
-------------------------------------------------------------------------------------
CheckedCollection
  -- CheckedQueue
  -- CheckedList
  -- CheckedRandomAccessList
  -- CheckedSet
  -- CheckedSortedSet
  -- CheckedNavigableSet
  -- CheckedMap
  -- CheckedSortedMap
  -- CheckedNavigableMap
源码没什么可看的,以集合空为前提重写集合各种操作方法。
-------------------------------------------------------------------------------------
Empty *
  -- EmptyList
  -- EmptySet
  -- EmptyMap 

EmptyIterator
EmptyListIterator
EmptyEnumeration
// 看一个 List
    private static class SingletonList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable {

        private static final long serialVersionUID = 3093736618740652951L;

        private final E element;  // 仅仅使用了一个元素代替了整个集合
  
        SingletonList(E obj)                {element = obj;}

        public Iterator<E> iterator() {
            return singletonIterator(element);
        }

        public int size()                   {return 1;}

        public boolean contains(Object obj) {return eq(obj, element);}

        public E get(int index) {
            if (index != 0)
              throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
            return element;
        }
       ...... //  各种集合操作方法,实现前提均为集合中只有一个元素。
}
-------------------------------------------------------------------------------------
Singleton *
  -- SingletonList
  -- SingletonSet
  -- SingletonMap
private static class CopiesList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable
    {
        private static final long serialVersionUID = 2739099268398711800L;

        final int n;               // 元素的数量
        final E element;          // 唯一的元素

        CopiesList(int n, E e) {
            assert n >= 0;
            this.n = n;
            element = e;
        }
        ......  // 各种 List 操作方法。
}
    private static class SetFromMap<E> extends AbstractSet<E>
        implements Set<E>, Serializable
    {
        private final Map<E, Boolean> m;  // The backing map
        private transient Set<E> s;       // Its keySet

        SetFromMap(Map<E, Boolean> map) {
            if (!map.isEmpty())
                throw new IllegalArgumentException("Map is non-empty");
            m = map;
            s = map.keySet();
        }
        ......  // 方法
}

还不知道用途在哪里...
不贴源码了...

Collections 常用方法

JDK API GO ! ~

上一篇 下一篇

猜你喜欢

热点阅读