在 Java 中,Collection
接口是 Java 集合框架中的根接口,它定义了一组操作来处理一组对象,即集合。Collection
接口的主要目的是提供一种统一的方式来操作和处理集合,无论是使用列表、集、队列还是映射等。
以下是关于 Collection
接口的基本介绍和细节讨论:
-
基本介绍:
Collection
接口位于java.util
包中。- 它是所有集合类的父接口,包括
List
、Set
等。 Collection
接口继承自Iterable
接口,因此支持迭代操作。
-
继承关系:
Collection
接口继承自Iterable
接口,因此它可以被遍历。- 它派生了两个主要的子接口:
List
和Set
。 List
接口表示有序的集合,允许重复元素。Set
接口表示无序的集合,不允许重复元素。
-
常用方法:
Collection
接口定义了一系列的方法,用于操作和管理集合,包括但不限于以下几种:add(E e)
:向集合中添加元素。remove(Object o)
:从集合中移除指定元素。contains(Object o)
:判断集合是否包含指定元素。size()
:返回集合中元素的数量。isEmpty()
:判断集合是否为空。addAll(Collection c)
:向集合中一次性添加多个元素containsAll(Collection c)
:判断集合是否包含指定元素(多个)。remove(Object o)
:从集合中一次性移除多个指定的元素。iterator()
:返回一个用于遍历集合的迭代器。
-
使用场景:
Collection
接口的具体实现类提供了不同的集合类型,可以根据不同的需求选择合适的实现类。例如,使用ArrayList
实现类来创建动态数组,使用HashSet
实现类来创建无序集合等。- 当你需要处理一组对象,并且关心元素的存储和顺序时,可以使用
Collection
接口的子接口List
。 - 当你需要处理一组对象,但不关心元素的顺序且不希望有重复元素时,可以使用
Collection
接口的子接口Set
。
总之,Collection
接口是 Java 集合框架的基础,定义了操作和处理集合的通用方法。它的实现类提供了不同类型的集合,适用于不同的需求场景。
常用方法代码:
public class CollectionMethods {public static void main(String[] args) {Collection collection = new ArrayList();//add添加单个元素collection.add("hello");collection.add(10);//这边有一个自动装箱的过程->collection.add(new Integer(10))collection.add(true);//同样有自动装箱System.out.println(collection);//remove删除指定元素//只能指定删除,不能通过下标删除,List中才能通过下标删除collection.remove(10);System.out.println(collection);//contains查找元素是否存在,存在返回true,不存在返回falseSystem.out.println(collection.contains(true));//size获取元素的个数System.out.println(collection.size());//isEmpty判断是否为空,为空返回true,不为空返回falseSystem.out.println(collection.isEmpty());//addAll一次性添加多个元素Collection collection2 = new ArrayList();collection2.add("hello");collection2.add("ret");collection.addAll(collection2);System.out.println(collection);//containsAll一次性查找多个元素是否都存在,都存在返回true,否则返回false//collection.remove("ret");//注释取消下面就为falseSystem.out.println(collection.containsAll(collection2));//removeAll一次性删除多个元素collection.removeAll(collection2);//这里的两个hello都会被删除System.out.println(collection);}
}
迭代器的使用代码:
public class Iterator_ {@SuppressWarnings({"all"})public static void main(String[] args) {Books[] books = new Books[4];books[0] = new Books("红楼梦","曹雪芹",99.0);books[1] = new Books("西游记","吴承恩",95.0);books[2] = new Books("水浒传","施耐庵",98.0);books[3] = new Books("三国演义","罗贯中",100.0);ArrayList arrayList= new ArrayList();arrayList.add(books[0]);arrayList.add(books[1]);arrayList.add(books[2]);arrayList.add(books[3]);Iterator iterator = arrayList.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}System.out.println("---------------重置迭代器后再次遍历---------------");iterator = arrayList.iterator();//快捷键ititwhile (iterator.hasNext()){System.out.println(iterator.next());}//快捷键ISystem.out.println("使用增强for循环,其底层仍然调用的是迭代器,可以将其理解为一个简单的迭代器");for (Object book:arrayList) {System.out.println(book);}}
}class Books{private String name;private String author;private double price;public Books() {}public Books(String name, String author, double price) {this.name = name;this.author = author;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Books{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +'}';}
}