集合概述
- 集合:集合是java中提供的一种容器,可以用来存储多个数据。
集合和数组的区别
- 数组的长度是固定的。集合的长度是可变的。
- 数组中存储的是同一类型的元素,可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不一致。在开发中一般当对象多的时候,使用集合进行存储。
集合框架
JAVASE提供了满足各种需求的API,在使用这些API前,先了解其继承与接口操作架构,才能了解何时采用哪个类,以及类之间如何彼此合作,从而达到灵活应用。
集合按照其存储结构可以分为两大类,分别是单列集合java.util.Collection
和双列集合java.util.Map
- Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是
java.util.List
和java.util.Set
。其中,List
的特点是元素有序、元素可重复。Set
的特点是元素无序,而且不可重复。List
接口的主要实现类有java.util.ArrayList
和java.util.LinkedList
,Set
接口的主要实现类有java.util.HashSet
和java.util.TreeSet
。
整个集合类的继承关系
集合本身是一个工具,它存放在java.util包中。在Collection
接口定义着单列集合框架中最最共性的内容。
Collection 常用功能
Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:
public boolean add(E e)
: 把给定的对象添加到当前集合中 。public void clear()
:清空集合中所有的元素。public boolean remove(E e)
: 把给定的对象在当前集合中删除。public boolean contains(E e)
: 判断当前集合中是否包含给定的对象。public boolean isEmpty()
: 判断当前集合是否为空。public int size()
: 返回集合中元素的个数。public Object[] toArray()
: 把集合中的元素,存储到数组中。
代码演示
import java.util.ArrayList;
import java.util.Collection;
//顶层接口Collection,包含底层接口的共性方法
/*
* `public boolean add(E e)`: 把给定的对象添加到当前集合中 。
* `public void clear()` :清空集合中所有的元素。
* `public boolean remove(E e)`: 把给定的对象在当前集合中删除。
* `public boolean contains(E e)`: 判断当前集合中是否包含给定的对象。
* `public boolean isEmpty()`: 判断当前集合是否为空。
* `public int size()`: 返回集合中元素的个数。
* `public Object[] toArray()`: 把集合中的元素,存储到数组中。
*/
public class Demo01Collection {
public static void main(String[] args) {
//创建对象,可以使用泛型
Collection<String> collection = new ArrayList<>();
//向集合中添加元素
boolean isAdded = collection.add("伊泽瑞尔");
collection.add("魔法猫咪");
collection.add("瑞兹");
collection.add("李青");
collection.add("亚托克斯");
collection.add("锤石");
//判断是否添加成功
System.out.println("是否添加成功:"+isAdded);//true
System.out.println(collection);//[伊泽瑞尔, 魔法猫咪, 瑞兹, 李青, 亚托克斯, 锤石]
//删除锤石
boolean isRemoved = collection.remove("锤石");
//判断是否删除成功
System.out.println("是否删除成功:"+isRemoved);//true
System.out.println(collection);//[伊泽瑞尔, 魔法猫咪, 瑞兹, 李青, 亚托克斯]
//判断集合中是否含有伊泽瑞尔
boolean isContain = collection.contains("伊泽瑞尔");
System.out.println("集合中是否包含伊泽瑞尔:"+isContain);//true
//判断集合是否为空
boolean isEmpty = collection.isEmpty();
System.out.println("集合是否为空:"+isEmpty);//false
//输出集合中元素的个数
int size = collection.size();
System.out.println("集合中元素个数:"+size);//5
//将集合转换为数组
Object[] array = collection.toArray();
//遍历数组
System.out.print("遍历数组结果: ");
for (int i = 0; i <array.length ; i++) {
System.out.print(array[i]+" ");
}
System.out.println();
//清空集合中所有元素
collection.clear();
System.out.println("清空结果:"+collection);
}
}
结果截图:
集合遍历
Iterator接口
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口java.util.Iterator
。Iterator
接口也是Java集合中的一员,但它与Collection
、Map
接口有所不同,Collection
接口与Map
接口主要用于存储元素,而Iterator
主要用于迭代访问(即遍历)Collection
中的元素,因此Iterator
对象也被称为迭代器。
想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作,下面介绍一下获取迭代器的方法:
public Iterator iterator()
: 获取集合对应的迭代器,用来遍历集合中的元素的。
下面介绍一下迭代的概念:
- 迭代:即Collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。
Iterator接口的常用方法如下:
public E next()
:返回迭代的下一个元素。public boolean hasNext()
:如果仍有元素可以迭代,则返回 true。
tips::在进行集合元素取出时,如果集合中已经没有元素了,还继续使用迭代器的next方法,将会发生java.util.NoSuchElementException没有集合元素的错误。
迭代器的实现原理
我们在之前案例已经完成了Iterator遍历集合的整个过程。当遍历集合时,首先通过调用t集合的iterator()方法获得迭代器对象,然后使用hashNext()方法判断集合中是否存在下一个元素,如果存在,则调用next()方法将元素取出,否则说明已到达了集合末尾,停止遍历元素。
Iterator迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素。
在调用Iterator的next方法之前,迭代器的索引位于第一个元素之前,不指向任何元素,当第一次调用迭代器的next方法后,迭代器的索引会向后移动一位,指向第一个元素并将该元素返回,当再次调用next方法时,迭代器的索引会指向第二个元素并将该元素返回,依此类推,直到hasNext方法返回false,表示到达了集合的末尾,终止对元素的遍历。
增强for
增强for循环(也称for each循环)是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作。
格式:
for(元素的数据类型 变量 : Collection集合or数组){
//写操作代码
}
它用于遍历Collection和数组。通常只进行遍历元素,不要在遍历的过程中对集合元素进行增删操作。
代码演示
package com.company.demo06.Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02Traverse {
public static void main(String[] args) {
//创建集合
Collection collection=createCollection();
//用iterator遍历集合
traverseIterator(collection);
//用增强fro遍历集合
tranverseFor(collection);
}
private static void tranverseFor(Collection<String> collection) {
System.out.println("----------------增强for遍历------------------");
for (String c:
collection) {
System.out.print(c+" ");
}
}
/*
* `public E next()`:返回迭代的下一个元素。
* `public boolean hasNext()`:如果仍有元素可以迭代,则返回 true。
*/
private static void traverseIterator(Collection<String> collection) {
System.out.println("----------------Iterator遍历------------------");
//多态创建Iterator对象
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
}
private static Collection<String> createCollection() {
Collection<String > collection = new ArrayList<>();
//添加元素
collection.add("无尽之刃");
collection.add("饮血剑");
collection.add("破败王者之刃");
collection.add("幻影之舞");
collection.add("魔宗");
return collection;
}
}
结果
tips: 新for循环必须有被遍历的目标。目标只能是Collection或者是数组。新式for仅仅作为遍历操作出现。