一、Collection接口:
1.List接口:
(1)Collection表示一组对象,而List、Set是Collection的子接口;继承了Collection的方法。
(2)关系图:

(3)Collection的方法:

(4)List接口:
List是有序的、可重复的容器;List接口常用的实现类有三个:ArrayList,LinkedList、Vector。
(5)List常用方法:

2.ArrayList类:
(1)ArrayList类:
实现了List接口;List接口的大小可变数组的实现。实现了所有可选列表操作,并允许包括null在内的所有元素。除了实现List接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。
(2)特点:查询效率高,增删效率低,线程不安全。
(3)常用方法:
1)·add(int index,Object obj) 将指定的元素插入此列表中的指定位置;
2)get(int index)返回此列表中指定位置上的元素;
3)set(int indext,Object) 用指定的元素替代此列表中指定位置上的元素;
4)size()返回此列表中的元素数;
5)isEmpty()如果此列表中没有元素,则返回 true;
6)remove(int index)移除此列表中指定位置上的元素。


(4)ArrayList的用法:


3.LinkedList类:
LinkedList底层用双向链表实现的储存。
(1)特点:查询效率低,增删效率高,线程不安全。
(2)双向链表:
双向链表也叫双链表,是链表的一种,它的每个数据节点中都有两个指针,分别指向前一个节点和后一个节点。 所以,从双向链表中的任意一个节点开始,都可以很方便地找到所有节点。

每个节点的组成:

(3)LinkedList类的常用方法:
1)addFirst(E e):在该表的开头插入指定的元素。
2)addLast(E e):将指定的元素追加到列表的末尾;
3)getFirst():获取列表中第一个元素;
4)getLast():获取列表中最后一个元素;
5)removeFirst():移除第一个元素;
6)removeLast():移除最后一个元素;


4.Vector类:
(1) Vector底层是用数组实现的List,相关的方法都加了同步检查,因此“线程安全,效率低”。
(2)Vector和ArrayList的区别:
1) Vector 的 的 add() 方法是同步方法,ArrayList 的 的 add()方法是非同步方法;
2)Vector 扩容每次扩充 1 倍 ,ArrayList 每次扩充 0. 5倍;
3) Vector 是在调用构造方法时, , 直接初始化容量为10 ,ArrayList 是在第一次调用添加方法时,初始化容量为 10
4) Vector 是线程同步的,安全性高,效率低,ArrayList 是线程非同步的,安全性低,效率高。
(3)常用方法:
1)add(E e):将指定元素添加到此列表的结尾;
2)get(int index): 返回此列表中指定位置处的元素;
3)size(): 返回此列表的元素数;
4)remove(int index): 移除此列表中指定位置处的元素;
5)add(int index, E element): 在此列表中指定的位置插入指定的元素;


二、Map接口:
(1)实现 Map 接口的类用来存储键(key) -值(value)对;
(2) Map 接口的实现类有 HashMap 和 TreeMap 等;
(3) Map 类中存储的键-值对通过键来标识,所以键值不能重复。
Map接口常用的方法:

关系图:

1.HashMap类:
(1)HashMap采用哈希算法实现,是Map接口最常用的实现类。由于底层采用了哈希表存储数据,要求键不能重复,如果发生重复,新的键值对会替换旧的键值对。 HashMap在查找、删除、修改方面都有非常高的效率。
(2)特点: HashMap: 线程不安全,效率高。允许key或value为null。
(3)HashMap的存储原理:

(4)HashMap的常用方法:
1)isEmpty(): 如果此映射不包含键-值映射关系,则返回 true。
2)keySet(): 返回此映射中所包含的键的 Set 视图。
3)put(K key, V value):在此映射中关联指定值与指定键;
4)containsKey(Object key):如果此映射包含对于指定键的映射关系,则返回 true。
5)containsKey(Object key): 如果此映射包含对于指定键的映射关系,则返回 true。


2.Hashtable类:
(1)HashTable类和HashMap用法几乎一样;但是HashTable的方法添加了synchronized关键字确保线程同步检查,效率较低。
(2)特点: 线程安全,效率低。不允许key或value为null;
(3)常用方法:
1)get(Object key):返回指定键所映射到的值,如果此映射不包含此键的映射,则返回 null. 更确切地讲,如果此映射包含满足 (key.equals(k)) 的从键 k 到值 v 的映射,则此方法返回 v;否则,返回 null;
2)isEmpty():测试此哈希表是否没有键映射到值。
3)containsKey(Object key):测试指定对象是否为此哈希表中的键。
4)containsValue(Object value):如果此 Hashtable 将一个或多个键映射到此值,则返回 true。
5)contains(Object value): 测试此映射表中是否存在与指定值关联的键。


网友评论