Map Set List

研究Java中的各种集合之间的区别以及如何使用。


Alt Text
集合结构图

General

Java 中有两个接口,一个是Collection,另一个是Map。Set和List都是Collection的子类(Set and List implement from Collection)。在Java中,我们不可以直接使用Collection,使用的都是继承自Collection的子类(Set,List)。

Collection主要方法:

boolean add(Object o)添加对象到集合

boolean remove(Object o)删除指定的对象

int size()返回当前集合中元素的数量

boolean contains(Object o)查找集合中是否有指定的对象

boolean isEmpty()判断集合是否为空

Iterator iterator()返回一个迭代器

boolean containsAll(Collection c)查找集合中是否有集合c中的元素

boolean addAll(Collection c)将集合c中所有的元素添加给该集合

void clear()删除集合中所有元素

void removeAll(Collection c)从集合中删除c集合中也有的元素

void retainAll(Collection c)从集合中删除集合c中不包含的元素

Set

HashSet和TreeSet是两个最常见的继承自Set的类。

List

LinkedList, ArrayList

Yingbo Yuan

Yingbo Yuan

Be Patient

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora