在 android 项目开发过程当中,总会使用到 Collection,对于一些基础的使用方法仍是能够的,可是涉及到较深层次的就有点力不从心了,因此打算开始完全地学习一下 java 集合方面的知识点,作个记录总结,java
分析工具android
JavaJDK:1.7.0_79
AndroidStudio:3.5
复制代码
首先来看下 java 集合框架的总图: git
Collection 是 List、Set 等集合高度抽象出来的接口,它包含了这些集合的基本操做,它主要又分为两大部分:List和Set。github
List 接口一般表示一个列表(数组、队列、链表、栈等),其中的元素能够重复,经常使用实现类为 ArrayList 和 LinkedList,另外还有不经常使用的 Vector。另外,LinkedList 仍是实现了 Queue 接口,所以也能够做为队列使用。设计模式
Set 接口一般表示一个集合,其中的元素不容许重复(经过 hashcode 和 equals 函数保证),经常使用实现类有 HashSet 和 TreeSet,HashSet 是经过 Map 中的HashMap 实现的,而 TreeSet 是经过 Map 中的 TreeMap 实现的。另外,TreeSet 还实现了 SortedSet 接口,所以是有序的集合(集合中的元素要实现 Comparable 接口,并覆写 Compartor 函数才行)。数组
咱们看到,抽象类 AbstractCollection、AbstractList 和 AbstractSet 分别实现了Collection、List 和 Set 接口,这就是在 Java 集合框架中用的不少的适配器设计模式,用这些抽象类去实现接口,在抽象类中实现接口中的若干或所有方法,这样下面的一些类只需直接继承该抽象类,并实现本身须要的方法便可,而不用实现接口中的所有抽象方法。bash
/**
* {@code AbstractList} is an abstract implementation of the {@code List} interface, optimized
* for a backing store which supports random access. This implementation does
* not support adding or replacing. A subclass must implement the abstract
* methods {@code get()} and {@code size()}, and to create a
* modifiable {@code List} it's necessary to override the {@code add()} method that * currently throws an {@code UnsupportedOperationException}. * * @since 1.2 */ public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {} 复制代码
/**
* An AbstractSet is an abstract implementation of the Set interface. This
* implementation does not support adding. A subclass must implement the
* abstract methods iterator() and size().
*
* @since 1.2
*/
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}
复制代码
/**
* Class {@code AbstractCollection} is an abstract implementation of the {@code
* Collection} interface. A subclass must implement the abstract methods {@code
* iterator()} and {@code size()} to create an immutable collection. To create a
* modifiable collection it's necessary to override the {@code add()} method that * currently throws an {@code UnsupportedOperationException}. * * @since 1.2 */ public abstract class AbstractCollection<E> implements Collection<E>{} 复制代码
Map 是一个映射接口,其中的每一个元素都是一个 key-value 键值对,一样抽象类 AbstractMap 经过适配器模式实现了 Map 接口中的大部分函数,TreeMap、HashMap、WeakHashMap 等实现类都经过继承 AbstractMap 来实现,另外,不经常使用的 HashTable 直接实现了 Map 接口,它和 Vector 都是 JDK1.0 就引入的集合类。框架
/*
* @since 1.2
*/
public class TreeMap<K, V> extends AbstractMap<K, V>
implements SortedMap<K, V>, NavigableMap<K, V>, Cloneable, Serializable {}
复制代码
public interface NavigableMap<K,V> extends SortedMap<K,V> {}
复制代码
/**
* A map that has its keys ordered. The sorting is according to either the
* natural ordering of its keys or the ordering given by a specified comparator.
*/
public interface SortedMap<K,V> extends Map<K,V> {}
复制代码
public class HashMap<K, V> extends AbstractMap<K, V> implements Cloneable, Serializable {}
复制代码
/*
* @since 1.2
* @see HashMap
* @see WeakReference
*/
public class WeakHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {}
复制代码
Iterator 是遍历集合的迭代器(不能遍历 Map,只用来遍历 Collection),Collection 的实现类都实现了 iterator() 函数,它返回一个 Iterator 对象,用来遍历集合,ListIterator 则专门用来遍历 List。而 Enumeration 则是 JDK1.0 时引入的,做用与 Iterator 相同,但它的功能比 Iterator 要少,它只能再 Hashtable、Vector 和 Stack 中使用。dom
/**
* An ListIterator is used to sequence over a List of objects. ListIterator can
* move backwards or forwards through the list.
*/
public interface ListIterator<E> extends Iterator<E> {}
复制代码
以上即是集合的主要内容,咱们下面的系列都是按照上面的内容进行详细的分析和总结,欢迎持续关注~ide
专一于 Android 开发多年,喜欢写 blog 记录总结学习经验,blog 同步更新于本人的公众号,欢迎你们关注,一块儿交流学习~