2、集合--Collection、Map、Set、List、Compareable、Iterator接口的源码
首先对Connection的源码进行分析:
public interface Collection<E> extends Iterable<E> { //返回当前集合中元素的数量 int size();
//判断集合中是否含有元素 boolean isEmpty();
//查找集合中是由含有对象o boolean contains(Object o);
//返回一个迭代器用于访问集合中的每一个集合 Iterator<E> iterator();
//返回一个内涵集合所有元素的array Object[] toArray();
//返回一个内含所有元素的array。运行期间返回的array和a的类型相同,需要转换为正确的类型 <T> T[] toArray(T[] a);
//将对象添加到集合 boolean add(E e);
//如果集合中的有与元素o相匹配的对象,将其删除 boolean remove(Object o);
//查找的集合中的元素是否含有集合c中的所有元素 boolean containsAll(Collection<?> c);
//将集合c中的yuans都添加到集合中 boolean addAll(Collection<? extends E> c);
//从集合中删除所有的c集合中所包含的元素 boolean removeAll(Collection<?> c);
default boolean removeIf(Predicate<? super E> filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator<E> each = iterator(); while (each.hasNext()) { if (filter.test(each.next())) { each.remove(); removed = true; } } return removed; }
//从集合中删除集合c中没有的元素 boolean retainAll(Collection<?> c);
//删除集合种的所有元素 void clear(); boolean equals(Object o); int hashCode(); @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, 0); } default Stream<E> stream() { return StreamSupport.stream(spliterator(), false); } default Stream<E> parallelStream() { return StreamSupport.stream(spliterator(), true); } }
Iterator源码
public interface Iterator<E> { //判断是否存在另一个可访问的接口 boolean hasNext();
//返回要访问的下一个元素 E next(); //删除上次访问的对象 default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
List源码
public interface List<E> extends Collection<E> {
//返回当前集合中元素的数量 int size(); //判断当前元素是否含有元素 boolean isEmpty(); //查找集合中是否包含对象o boolean contains(Object o); //返回一个内含集合中元素的array Object[] toArray();
//返回一个内含所有元素的array,运行期间返回array和a类型相同,需要转换为正确的类型 <T> T[] toArray(T[] a); //将兑现e添加到集合 boolean add(E e); //如果集合中有与元素o相匹配的对象,将其删除 boolean remove(Object o); //查找集合中的元素是否含有集合c中的所有元素 boolean containsAll(Collection<?> c); //将集合c中的元素都添加到集合中 boolean addAll(Collection<? extends E> c); //将集合中c中元素添加在指定的位置 boolean addAll(int index, Collection<? extends E> c); //从集合中删除c集合包含的所有元素 boolean removeAll(Collection<?> c); //从集合中删除c中元素不包含的元素 boolean retainAll(Collection<?> c); default void replaceAll(UnaryOperator<E> operator) { Objects.requireNonNull(operator); final ListIterator<E> li = this.listIterator(); while (li.hasNext()) { li.set(operator.apply(li.next())); } } @SuppressWarnings({"unchecked", "rawtypes"}) default void sort(Comparator<? super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } }
//删除集合中的所有元素 void clear(); boolean equals(Object o); int hashCode(); //返回指定位置的元素 E get(int index); //用元素element元素代替index位置上的元素 E set(int index, E element); void add(int index, E element); //删除指定位置上的元素 E remove(int index); //返回第一次出现元素o的位置,如果没有元素o则返回-1 int indexOf(Object o); //返回最后一次出现元素o的位置,如果没有该元素则返回-1 int lastIndexOf(Object o); //返回一个列表迭代器,用来访问列表中的元素 ListIterator<E> listIterator(); //返回一个列表迭代器,用来从指定位置index开始访问列表中的元素 ListIterator<E> listIterator(int index); //返回指定位置fromIndex到toIndex范围中各个元素的列表视图 List<E> subList(int fromIndex, int toIndex); @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, Spliterator.ORDERED); } }
Set接口:
各个方法和上述的方法基本一致
public interface Set<E> extends Collection<E> {
// int size(); boolean isEmpty(); boolean contains(Object o);
Iterator<E> iterator(); Object[] toArray(); <T> T[] toArray(T[] a); boolean add(E e); boolean remove(Object o); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean retainAll(Collection<?> c); boolean removeAll(Collection<?> c); void clear(); boolean equals(Object o); int hashCode(); @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, Spliterator.DISTINCT); } }
Map源码:
public interface Map<K,V> { //返回当前映像中映射的数量 int size();
//判断当前映像中映射的数量 boolean isEmpty();
//判断映像中是否存在关键字key boolean containsKey(Object key);
//判断当前映像中是否存在值value boolean containsValue(Object value); //获得与关键字key相关的值,并且返回与关键字key相关的对象
如果没有在该映像中找到关键字,则返回null V get(Object key); //将相互关联的一个关键字与一个值放入该映像
如果关键之已经存在,那么旧值会被新值取代 V put(K key, V value); //从映像中删除与key相关的映射 V remove(Object key); //将来自特定映像的所有元素添加给该对象 void putAll(Map<? extends K, ? extends V> m);
//清除所有的映像 void clear(); //返回映像中所有的关键字视图集 Set<K> keySet(); //返回映像中所有值的视图集 Collection<V> values(); //返回Map.Entry对象的视图集 Set<Map.Entry<K, V>> entrySet(); interface Entry<K,V> { K getKey(); V getValue(); V setValue(V value); boolean equals(Object o); int hashCode(); public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() { return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); } public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() { return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getValue().compareTo(c2.getValue()); } public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) { Objects.requireNonNull(cmp); return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); } public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) { Objects.requireNonNull(cmp); return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); } } boolean equals(Object o); int hashCode(); default V getOrDefault(Object key, V defaultValue) { V v; return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue; } default void forEach(BiConsumer<? super K, ? super V> action) { Objects.requireNonNull(action); for (Map.Entry<K, V> entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } } default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { Objects.requireNonNull(function); for (Map.Entry<K, V> entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } // ise thrown from function is not a cme. v = function.apply(k, v); try { entry.setValue(v); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } } } default V putIfAbsent(K key, V value) { V v = get(key); if (v == null) { v = put(key, value); } return v; } default boolean remove(Object key, Object value) { Object curValue = get(key); if (!Objects.equals(curValue, value) || (curValue == null && !containsKey(key))) { return false; } remove(key); return true; } default boolean replace(K key, V oldValue, V newValue) { Object curValue = get(key); if (!Objects.equals(curValue, oldValue) || (curValue == null && !containsKey(key))) { return false; } put(key, newValue); return true; } default V replace(K key, V value) { V curValue; if (((curValue = get(key)) != null) || containsKey(key)) { curValue = put(key, value); } return curValue; } default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { Objects.requireNonNull(mappingFunction); V v; if ((v = get(key)) == null) { V newValue; if ((newValue = mappingFunction.apply(key)) != null) { put(key, newValue); return newValue; } } return v; } default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); V oldValue; if ((oldValue = get(key)) != null) { V newValue = remappingFunction.apply(key, oldValue); if (newValue != null) { put(key, newValue); return newValue; } else { remove(key); return null; } } else { return null; } } default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); V oldValue = get(key); V newValue = remappingFunction.apply(key, oldValue); if (newValue == null) { // delete mapping if (oldValue != null || containsKey(key)) { // something to remove remove(key); return null; } else { // nothing to do. Leave things as they were. return null; } } else { // add or replace old mapping put(key, newValue); return newValue; } } default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if(newValue == null) { remove(key); } else { put(key, newValue); } return newValue; } }
Compareable接口
@FunctionalInterface public interface Comparator<T> {
//能够对o1和2两个对象进行比较,如果o1位于o2的前面则返回负值
如果在排序中认为o1和o2是相同的值,则返回0
如果o1位于o2的后面则返回正值 int compare(T o1, T o2); //指示对象是否和obj相同 boolean equals(Object obj); default Comparator<T> reversed() { return Collections.reverseOrder(this); } default Comparator<T> thenComparing(Comparator<? super T> other) { Objects.requireNonNull(other); return (Comparator<T> & Serializable) (c1, c2) -> { int res = compare(c1, c2); return (res != 0) ? res : other.compare(c1, c2); }; } default <U> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { return thenComparing(comparing(keyExtractor, keyComparator)); } default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) { return thenComparing(comparing(keyExtractor)); } default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) { return thenComparing(comparingInt(keyExtractor)); } default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) { return thenComparing(comparingLong(keyExtractor)); } default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); } public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); } @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; } public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) { return new Comparators.NullComparator<>(true, comparator); } public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) { return new Comparators.NullComparator<>(false, comparator); } public static <T, U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (Comparator<T> & Serializable) (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); } public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); } public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); } public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); } }
以上是对主要的6个接口进行分析
其余的在之后的测试中有需要时在进行分析