P1 Collection接口和常用方法
一、Collection接口实现类的特点
public interface Collection extends Iterable

- Collection实现子类可以存放多个元素,每个元素可以是Object。
- 有些Collection的实现类,可以存放重复的元素,有些不可以。
- 有些Collection的实现类,有些是有序的(如:List),有些不是有序的(如:Set)。
- Collection接口没有直接的实现子类,是通过它的子接口Set 和 List来实现的。
二、常用方法
- add:添加单个元素
- remove:删除指定元素
- contains:查找元素是否存在
- size:获取元素个数
- isEmpty:判断是否为空
- clear:清空
- addAll:添加多个元素
- containsAll:查找多个元素是否都存在
- removeAll:删除多个元素
- //代码演示:
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author
- * @version 1.0
- */
- public class CollectionMethod {
- @SuppressWarnings({"all"})
- public static void main(String[] args) {
- //Collecton接口常用方法,以实现子类ArrayList来演示
- List list = new ArrayList();
- // add:添加单个元素
- list.add("jack");
- list.add("Tom");
- list.add(10);//有自动装箱的过程,相当于list.add(new Integer(10));
- list.add(true);
- System.out.println("list = " + list);
- // remove:删除指定元素
- list.remove(0);//删除第一个元素
- System.out.println(list.remove(true));//返回是否删除成功
- System.out.println("list = "+list);
- // contains:查找元素是否存在
- System.out.println(list.contains("Tom"));
- // size:获取元素个数
- System.out.println(list.size());
- // isEmpty:判断集合是否为空
- System.out.println(list.isEmpty());
- // clear:清空集合
- list.clear();
- System.out.println("list = " + list);
- // addAll:添加多个元素,实现了Collection接口的类的对象都可以传入
- ArrayList list2 = new ArrayList();
- list2.add("红楼梦");
- list2.add("三国演义");
- list.addAll(list2);
- System.out.println("list = "+ list);
- // containsAll:查找多个元素是否都存在
- System.out.println(list.containsAll(list2));
- // removeAll:删除多个元素
- list.add("聊斋");
- list.removeAll(list2);
- System.out.println("list = " + list);
- }
- }
- /*
- 运行结果:
- list = [jack, Tom, 10, true]
- true
- list = [Tom, 10]
- true
- 2
- false
- list = []
- list = [红楼梦, 三国演义]
- true
- list = [聊斋]
- */
复制代码 三、Collection接口遍历元素方式1 —— 使用 iterator(迭代器)
- 基本介绍:

- Iterator对象称为迭代器,主要用于遍历 Collection 集合中的元素。
- 所有实现了 Collection 接口的集合类都有都有一个 iterator() 方法,用以返回一个实现了 Iterator接口的对象,即可以返回一个迭代器。
- Iterator 仅用于遍历集合,Iterator 本身并不存放对象。
- Iterator的结构:
- 迭代器的执行原理:
- Iterator iterator = coll.iterator();//得到一个集合的迭代器
- while (iterator.hasNext()) {
- //hasNext():判断是否还有下一个元素;
- System.out.println(iterator.next());
- //next()作用:1. 下移 2. 将下移以后集合位置上的元素返回
- }
复制代码
- Iterator接口的方法


注意:在调用 it.next()方法前必须要调用 it.hasNext() 进行检测。若不调用,且下一条记录无效,直接调用 it.next() 会抛出 NoSuchElementException异常。
- 代码演示:
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- /**
- * @author
- * @version 1.0
- */
- public class CollectionIterator {
- @SuppressWarnings({"all"})
- public static void main(String[] args) {
- //Collection接口遍历对象方式1:迭代器遍历
- Collection col = new ArrayList();
- col.add(new Book("三国演义","罗贯中",10.1));
- col.add(new Book("红楼梦","曹雪芹",20.1));
- col.add(new Book("西游记","吴承恩",30.1));
- //System.out.println(col);
- //现在希望能够遍历 col集合
- //1. 先得到 col 对应的 迭代器
- Iterator iterator = col.iterator();
- //2. 使用 while循环遍历
- // while (iterator.hasNext()){//判断是否还有数据
- // //返回下一个元素,类型是Object
- // Object obj = iterator.next();
- // System.out.println(obj);
- // }
- //这里有一个快捷键,可以快速生成 while循环 =》itit
- //显示所有快捷键的快捷键 ctrl + j
- //itit
- while (iterator.hasNext()) {
- Object next = iterator.next();
- System.out.println(next);
- }
- //3. 当退出while循环后,这时 iterator迭代器 指向了最后的元素
- //iterator.next();//这里再使用next()就会抛出异常java.util.NoSuchElementException
- //4. 如果希望再次遍历,就需要重置iterator迭代器
- iterator = col.iterator();
- System.out.println("第二次遍历");
- while (iterator.hasNext()) {
- Object obj = iterator.next();
- System.out.println(obj);
- }
- }
- }
- class Book{
- private String name;
- private String author;
- private double price;
- public Book(String name, String author, double price) {
- this.name = name;
- this.author = author;
- this.price = price;
- }
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- '}';
- }
- }
- /*
- 运行结果:
- Book{name='三国演义', author='罗贯中', price=10.1}
- Book{name='红楼梦', author='曹雪芹', price=20.1}
- Book{name='西游记', author='吴承恩', price=30.1}
- 第二次遍历
- Book{name='三国演义', author='罗贯中', price=10.1}
- Book{name='红楼梦', author='曹雪芹', price=20.1}
- Book{name='西游记', author='吴承恩', price=30.1}
- */
复制代码 四、Collection接口遍历元素方式2 —— for循环增强
增强for循环,可以代替iterator迭代器。
特点:增强for就是简化版的iterator,本质一样,foreach的底层就是迭代器。只能用于遍历集合或数组。
- 基本语法:
- //for( 元素类型 元素名 : 集合名或数组名){
- // 访问元素
- //}
- for (Object object : col){
- System.out.println(object);
- }
复制代码 - 案例演示:
- import java.util.ArrayList;
- import java.util.Collection;
- public class CollectionFor {
- @SuppressWarnings({"all"})
- public static void main(String[] args) {
- Collection col = new ArrayList();
- col.add(new Book("三国演义","罗贯中",10.1));
- col.add(new Book("红楼梦","曹雪芹",20.1));
- col.add(new Book("西游记","吴承恩",30.1));
- //1. 使用增强for循环,在Collection集合
- //2. 增强for,底层仍然是迭代器
- //3. 增强for可以理解成就是简化版的迭代器
- for(Object book: col){
- System.out.println(book);
- }
- //增强for,也可以直接在数组使用
- int[] nums = {1,2,3,4,5,6};
- for (int i : nums){
- System.out.println(i);
- }
- //增强for 快捷键:I 或 col.for
- }
- }
- /*
- 运行结果:
- Book{name='三国演义', author='罗贯中', price=10.1}
- Book{name='红楼梦', author='曹雪芹', price=20.1}
- Book{name='西游记', author='吴承恩', price=30.1}
- 1
- 2
- 3
- 4
- 5
- 6
- */
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |