目录
(一)Iterable
[1. 介绍](#1. 介绍)
[2. 常见方法](#2. 常见方法)
(二)Collection
[1. 介绍](#1. 介绍)
[2. 常见方法](#2. 常见方法)
[(三) List](#(三) List)
[1. 介绍](#1. 介绍)
[2. 常见方法](#2. 常见方法)
总结

(一) Iterable
1. 介绍
Iterable接口是Java中的一个接口,它是集合框架中的根接口之一。Iterable接口表示实现了迭代功能,即可以通过迭代器遍历其中元素的类。它定义了一个抽象方法iterator(),该方法返回一个实现了Iterator接口的迭代器对象,通过这个迭代器对象可以依次访问集合中的元素。
总的来说,Iterable接口为集合类提供了统一的遍历方式,使得集合类可以通过for-each循环来遍历其中的元素,提供了更加便利和统一的遍历方式。
使用迭代器遍历集合,例如:
2. 常见方法
- boolean hasNext():如果迭代器有更多元素,则返回true。
 - E next():返回迭代器中的下一个元素。
 
下面是一个简单的示例,演示了如何使用迭代器遍历集合:
            
            
              java
              
              
            
          
          import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        // 使用迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
        (二) Collection
1. 介绍
在Java中,Collection是一个接口,它代表一组对象,这些对象通常被称为集合。Collection接口提供了一组操作集合的方法,包括添加、删除、遍历、查找等操作。它是Java集合框架的基础,定义了所有集合类共同的行为。Collection接口有很多实现类,比如List、Set和Queue等
2. 常见方法
- int size():返回集合中的元素数量。
 - boolean isEmpty():如果集合为空,则返回true。
 - boolean contains(Object o):如果集合包含指定的元素,则返回true。
 - boolean add(E e):将指定的元素添加到集合中。
 - boolean remove(Object o):从集合中移除指定的元素(如果存在)。
 - Iterator<E> iterator():返回在集合上进行迭代的迭代器。
 
下面是一个简单的示例,演示了如何使用Collection接口:
            
            
              java
              
              
            
          
          import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
class Main {
    public static void main(String[] args) {
        // 创建一个ArrayList对象,它是Collection接口的实现类
        Collection<String> collection = new ArrayList<>();
        // 添加元素
        collection.add("apple");
        collection.add("banana");
        collection.add("cherry");
        // 遍历集合 -- 使用for-each循环遍历集合
        for (String element : collection) {
            System.out.println(element);
        }
        // 获取集合中元素的个数
        int size = collection.size();
        System.out.println("Size: " + size);
        // 判断集合是否为空
        boolean isEmpty = collection.isEmpty();
        System.out.println("Is empty: " + isEmpty);
        // 判断集合中是否包含指定元素
        boolean contains = collection.contains("banana");
        System.out.println("Contains 'banana': " + contains);
        // 从集合中移除元素
        collection.remove("banana");
        // 再次遍历集合 -- 使用迭代器遍历集合
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()){
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
        
(三) List
1. 介绍
在Java中,List是一个接口,规范了ArrayList 和 LinkedList中要实现的方法。它继承自Collection接口,代表一个有序、可重复的集合。
ArrayList:实现了List接口,底层为动态类型顺序表
LinkedList:实现了List接口,底层为双向链表
2. 常见方法
- boolean add(E e):将指定的元素添加到列表的末尾。
 - void add(int index, E element):将指定的元素插入到列表中的指定位置。
 - E get(int index):返回列表中指定位置的元素。
 - E remove(int index):移除列表中指定位置的元素。
 - int size():返回列表中的元素数量。
 - boolean isEmpty():如果列表为空,则返回true。
 - boolean contains(Object o):如果列表包含指定的元素,则返回true。
 - Iterator<E> iterator():返回在列表上进行迭代的迭代器。
 
下面是一个简单的示例,演示了如何使用List接口:
            
            
              java
              
              
            
          
          import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class Main {
    public static void main(String[] args) {
        // 创建一个ArrayList对象,它是List接口的实现类
        List<String> list = new ArrayList<>();
        // 将指定的元素添加到列表的末尾
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        // 将指定的元素插入到列表中的指定位置
        list.add(1, "orange");
        // 返回列表中指定位置的元素
        String secondElement = list.get(1);
        System.out.println("Second element: " + secondElement);
        // 移除列表中指定位置的元素
        list.remove(2);
        // 返回列表中的元素数量
        int size = list.size();
        System.out.println("Size: " + size);
        // 如果列表为空,则返回true
        boolean isEmpty = list.isEmpty();
        System.out.println("Is empty: " + isEmpty);
        // 如果列表包含指定的元素,则返回true
        boolean contains = list.contains("banana");
        System.out.println("Contains 'banana': " + contains);
        // 返回在列表上进行迭代的迭代器
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
        }
    }
}
        总结
综上所述,Iterable接口用于表示可以迭代的集合,Collection接口是一组对象的集合,List接口是有序的集合,可以根据索引访问其中的元素。