JAVA_基础集合Collection接口与Iterator迭代器(一)
Java集合框架
一、集合框架的概述
- 集合、数组都是对多个数据进行存储操作的结构,简称:
Java
容器。
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt
,.jpg
,.avi
,数据库中)。 - 数组在存储多个数据方面的特点:
- 一旦初始化后,其长度就确定了。
- 需要指定元素的类型。
比如:String[] arr;
int[] arr1;
Object[] arr2
。
- 数组在存储多个数据方面的缺点:
- 一旦初始化,其长度就不能修改。
- 数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不变,同时效率不高。
- 获取数组中实际元素的个数。
- 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
二、Java集合可分为 Collection 和 Map 两种体系
① Collection
接口:单列集合,定义了存取一组对象的方法的集合 。用来存储一个一个的对象。
List
:元素有序、可重复的集合
ArrayList
、LinkedList
、Vector
Set
:元素无序、不可重复的集合
HashSet
、LinkedHashSet
、TreeSet
② Map
接口:双列集合,保存具有映射关系“key-value对”的集合。用来存储一对的数据。
HashMap
、LinkedHashMap
、TreeMap
、Hashtable
、Properties
Collection接口中的方法使用
add(Object e)
:将元素e添加到集合coll
中。
size()
:获取添加的元素的个数。
addAll(Collection coll)
:将coll
集合中的元素添加到当前的集合中。
clear()
:清空集合元素。
isEmpty()
:判断当前集合是否为空。
public void testCollection(){
Collection coll = new ArrayList();
//add(Object e):将元素e添加到集合coll中
coll.add("AA");
coll.add("BB");
coll.add("CC");
coll.add(123);// 自动装箱了
coll.add(new Date());
//size():获取添加的元素的个数
System.out.println(coll.size());// 5
//addAll(Collection coll):将coll集合中的元素添加到当前的集合中
Collection coll2 = new ArrayList();
coll2.add("CC");
coll2.add(123);
coll.addAll(coll2);
System.out.println(coll.size());
System.out.println(coll);
//clear:清空集合元素
coll.clear();
// isEmpty():判断当前集合是否为空
System.out.println(coll.isEmpty());
}
contains(Object obj)
:是通过元素的equals方法来判断是否 是同一个对象。
向Collection
接口的实现类的对象中添加数据obj
时,要求obj
所在类要重写equals()
。
public void testCollection2() {
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(new String("Tom"));
coll.add(false);
Person p = new Person("Jerry", 20);
coll.add(p);
coll.add(new Person("Snake",20));
//contains():判断当前集合是否包含Obj
// 我们在判断时会调用obj对象所在的equals()。
boolean contains = coll.contains("AA");
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(p));
System.out.println(coll.contains(new Person("Snake", 20)));
}
containsAll(Collection c)
:也是调用元素的equals方法来比 较的。拿两个集合的元素挨个比较。
Collection coll1 = Arrays.asList(123,"AA");
System.out.println(coll.containsAll(coll1));// true
remove(Object obj)
:通过元素的equals
方法判断是否是 要删除的那个元素。只会删除找到的第一个元素 。
removeAll(Collection coll)
:取当前集合的差集 7、取两个集合的交集。
public void test3() {
// remove(Object obj)
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(false);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
boolean remove = coll.remove(123);
coll.remove(new Person("Jerry", 20));
System.out.println(remove);// true
System.out.println(coll);// [AA, false, Tom]
// removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
Collection coll1 = Arrays.asList("AA",new String("Tom"));
coll.removeAll(coll1);
System.out.println(coll);// [false]
}
retainAll(Collection coll)
:获取当前集合和coll
集合的交集,并返回给当前集合。
equals(Object obj)
:集合是否相等 。要向返回true,需要当前集合和形参集合的元素都相同。
public void test4() {
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(false);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
// coll.retainAll(Collection coll):获取当前集合和coll1集合的交集,并返回给当前集合。
Collection coll1 = Arrays.asList(123,"AA","Snake");
coll.retainAll(coll1);
System.out.println(coll);// [AA, 123]
// equals(Object obj):要向返回true,需要当前集合和形参集合的元素都相同。
Collection coll2 = new ArrayList();
coll2.add("AA");
coll2.add(123);
System.out.println(coll.equals(coll2));// true
}
hashCode():
返回当前对象的哈希值。
- 集合→ 数组:
toArray()
- 数组→ 集合:调用
Arrays
类的静态方法asList()
public void test5() {
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(false);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
// hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());
// 集合---> 数组: toArray()
Object[] array = coll.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
// 扩展:数组---> 集合:调用Arrays类的静态方法asList()
List<String> stringList = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(stringList);java
}
迭代器Iterator接口
GOF
给迭代器模式的定义为:提供一种方法访问一个容器(container
)对象中各个元 素,而又不需暴露该对象的内部细节。迭代器模式,就是为容器而生。类似于“公 交车上的售票员”、“火车上的乘务员”、“空姐”。
Iterator
对象称为迭代器(设计模式的一种),主要用于遍历 Collection
集合中的元素。
Collection
接口继承了java.lang.Iterable
接口,该接口有一个iterator()
方法,那么所 有实现了Collection
接口的集合类都有一个iterator()
方法,用以返回一个实现了 Iterator
接口的对象。
Iterator
仅用于遍历集合,Iterator
本身并不提供承装对象的能力。如果需要创建 Iterator
对象,则必须有一个被迭代的集合。
集合对象每次调用iterator()
方法都得到一个全新的迭代器对象,默认游标都在集合 的第一个元素之前。
使用迭代器Iterator
接口,遍历集合元素的。
内部的方法:hasNext()
和 next()
。
hasNext()
:判断是否还有下一个元素。next()
:①指针下移 ②将下移后集合位置上的元素返回。
public void test1() {
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(false);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
// iterator():返回Iterator接口实例,用于遍历集合元素。
Iterator iterator = coll.iterator();
// 方式一:
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
// 报异常:NoSuchElementException
System.out.println(iterator.next());
// 方式二:
for (int i = 0; i < coll.size(); i++) {
System.out.println(iterator.next());
}
// 方式三:推荐
// hasNext():判断是否还有下一个元素
while(iterator.hasNext()) {
// next():①指针下移 ②将下移后集合位置上的元素返回
System.out.println(iterator.next());
}
}
remove()
:可以在遍历的时候,删除集合中的元素。此方法不同于集合中的remove()。
注意:如果还未调用next()
或在上一次调用 next()
方法之后已经调用了 remove()
方法, 再调用remove()
都会报IllegalStateException()
。
public void test3() {
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(false);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
/** 删除集合中的"Tom" */
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
Object next = iterator.next();
if ("Tom".equals(next)){
iterator.remove();
}
}
/** 遍历集合 */
Iterator iterator1 = coll.iterator();
while (iterator1.hasNext()) {
System.out.println(iterator1.next());
}
}