1 集合类体系结构
集合分为:Collection(单列)和Map(双列)
Collection分为:List(可重复)和Set(不可重复)
List分为:ArrayList、LinkedList
Set分为:HashSet、TreeSet
Map分为:hashMap、TreeMap
2 Collection集合
(1)Collection集合是单列集合的顶层接口,表示一组被称为其元素的对象。
JDK不提供此接口任何,它提供更具体的子接口(如set和List)实现
(2)Collection集合常用方法
boolean add(E e) 添加元素
booleanremove(Object o) 移除指定的元素
void clear() 清空集合中的元素
boolean contains(Object o) 判断是否存在指定的元素
boolean isEmpty() 判断集合是否为空
int size() 集合中的元素个数
3 集合的遍历
(1)Iterator:迭代器,集合的专用遍历方式
Iterator<E> iterator():返回集合中元素的迭代器,通过集合的iterator()方法得到
(2)Iterator的常用方法
E next():返回迭代的下一个元素
boolean hasNext():如果还有更多元素,返回true
示例:
Collection<String> c = new ArrayList<String>();
c.add("床前明月光");
c.add("疑是地上霜");
c.add("举头望明月");
c.add("低头思故乡");
Iterator<String> iterator = c.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}