今天先来介绍Java集合中的老大类Collection接口,说说Collection、Iterable和Iterator三个接口之间的关系,为什么要封装一个Collection接口?我们今天来讨论讨论
目录
Collection、Iterable、Iterator三者之间有什么关系?
为什么定义出一个Collection接口?
Collection接口可以理解为是父接口,它里面定义了下面具体实现接口类的一些公共规范。比方说添加元素、删除元素都提供了统一的方法来实现。还为实现类List、Set提供了两个构造函数(void无参构造函数、具有指定容量的构造函数)。
Iterable是什么?
遍历集合的接口,提供了用于遍历集合的iterable方法,里面定义了用于for-each元素的具体逻辑,并且返回一个T类型的Iterator迭代器对象
Iterator是什么?
迭代器接口,可以用来遍历集合对象,提供了hashNext()、next()、remove()三个公共的操作接口,实现解耦合(隔离容器类的遍历操作和底层实现)
Collection、Iterable、Iterator三者之间有什么关系?
Collection:父接口,继承Iterable接口,提供了实现类统一的操作方法
Iterable:接口,提供了遍历元素的方法,返回一个T类型的Iterator对象
Iterator:迭代器接口,用来遍历集合对象执行的具体操作
下面我们来看看他们三者的具体应用
Collection接口
Collection中能存放什么元素?
没有使用“泛型”之前,Collection中可以存储Object的所有子类型。使用了“泛型”之后,Collection中只能存储某个具体的类型。
方法
int size();
boolean isEmpty();
void clear();
boolean contains(Object element);
boolean add(Object element);
boolean remove(Object element);
Iterator iterator();
boolean containsAll(Collection c);
boolean addAll(Coolection c);
boolean removeAll(Collection c);
boolean retainAll(Collection c);
Object[] toArray();
实战演练
import java.util.*;
public class CollectionTest {
public static void main(String[] args){
Collection c = new ArrayList();
//可以放入不同类型的对象
c.add("hello");
c.add(new Name("f1","l1"));
System.out.println(c.size());
System.out.println();
}
}
class Name {
private String firstName,lastName;
public Name(String firstName,String lastName){
this.firstName=firstName;
this.lastName =lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String toString(){
return firstName+" "+lastName;
}
}
import java.util.*;
public class BasicContainer {
public static void main(String[] args){
Collection c= new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l1")));
System.out.println(c);
}
}
class Name {
private String firstName,lastName;
public Name(String firstName,String lastName){
this.firstName=firstName;
this.lastName =lastName;
}
public boolean equals(Object obj){
if(obj instanceof Name){
Name name=(Name) obj;
return (firstName.equals((name.firstName)))
&& (lastName.equals(name.lastName));
}
return super.hashCode(){
return firstName.hashCode();
}
}
}
Iterator接口
统一遍历Collection元素的接口
- 迭代:重复执行某一个固定的过程,每一次迭代得到的结果作为下一次迭代的初始值
- 迭代器: 遍历集合的对象,为各种容器提供了公共操作接口,隔离对容器的遍历操作和底层实现,实现解耦
迭代器类似与foreach遍历,每次遍历集合中的一个元素,每次自增1
方法
hashNext()
next()
remove()
实战演练
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//添加元素
c.add("abc");
c.add("def");
c.add(100);
c.add(new Object());
//获取集合对象的迭代器对象Iterator
Iterator it = c.iterator();
//通过以上或取得迭代器想开始迭代/遍历集合
while (it.hasNext()) {
// 让迭代器前进一位,并且将 指向的元素返回
Object obj = it.next();
System.out.println(obj);
}
}
}
内存执行图
总结
Collection、Iterable和Iterator是Java编程语言中用于处理集合的接口。它们之间的区别如下:
-
Collection接口是所有集合类的根接口,它表示一组对象的集合,并提供了对集合进行操作的方法。它继承了Iterable接口。
-
Iterable接口是用于遍历集合中的元素的接口,它定义了一个用于返回迭代器的方法iterator()。实现了Iterable接口的对象可以使用foreach循环进行遍历。
-
Iterator接口是用于遍历集合中的元素的迭代器接口,它定义了用于遍历集合元素的方法,如hasNext()、next()和remove()。通过调用集合对象的iterator()方法,可以获取一个实现了Iterator接口的迭代器对象。
总结起来,Collection是一组对象的集合,Iterable是用于遍历集合的接口,Iterator是用于遍历集合元素的迭代器接口。Collection继承了Iterable接口,因此可以使用foreach循环进行遍历。Iterator接口提供了更灵活的遍历方式,可以手动控制遍历过程,并且可以在遍历过程中删除元素。