Java—Collection、Iterable、Iterator的区别

今天先来介绍Java集合中的老大类Collection接口,说说Collection、Iterable和Iterator三个接口之间的关系,为什么要封装一个Collection接口?我们今天来讨论讨论

目录

为什么定义出一个Collection接口?

Iterable是什么?

Iterator是什么?

Collection、Iterable、Iterator三者之间有什么关系?

Collection接口

Collection中能存放什么元素?

方法

实战演练

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编程语言中用于处理集合的接口。它们之间的区别如下:

  1. Collection接口是所有集合类的根接口,它表示一组对象的集合,并提供了对集合进行操作的方法。它继承了Iterable接口。

  2. Iterable接口是用于遍历集合中的元素的接口,它定义了一个用于返回迭代器的方法iterator()。实现了Iterable接口的对象可以使用foreach循环进行遍历。

  3. Iterator接口是用于遍历集合中的元素的迭代器接口,它定义了用于遍历集合元素的方法,如hasNext()、next()和remove()。通过调用集合对象的iterator()方法,可以获取一个实现了Iterator接口的迭代器对象。

总结起来,Collection是一组对象的集合,Iterable是用于遍历集合的接口,Iterator是用于遍历集合元素的迭代器接口。Collection继承了Iterable接口,因此可以使用foreach循环进行遍历。Iterator接口提供了更灵活的遍历方式,可以手动控制遍历过程,并且可以在遍历过程中删除元素。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乔努力变强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值