目录
1. Collection
1.1 集合类概述
集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变。
1.2 集合类体系结构

1.3 Collection集合概述和使用
Collection集合概述
- 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
- JDK不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现
创建Collection集合的对象
- 多态的方式
- 具体的实现类ArrayList
实例:
import java.util.ArrayList;
import java.util.Collection;
/*
* 创建Collection的对象
* 多态的方式
* ArrayList()*/
public class CollectionTest {
public static void main(String[] args) {
//创建Collection的对象
Collection<String> c = new ArrayList<String>();
//添加元素:boolean add(E e);
c.add("hello");
c.add("hello");
c.add("lucycia");
//输出集合对象
System.out.println(c);
}
}
控制台输出:
![]()
我们发现输出的是字符串,但是c对象是new出来的,说明已经重写了toString()方法
我们去查看源代码,首先查看ArrayList的继承关系

我们可以发现ArrayList继承了AbstractList类和一些接口,接口中不能重写方法,所以我们去AbstractList类中去寻找,结果发现AbstractList也没有重写toString方法,再去AbstractCollection类中去寻找,结果发现在这个类中重写了toString()方法

1.4 Collection集合常用方法
| 方法名 | 说明 |
| boolean add(E e) | 添加元素 |
| boolean remove(Object o) | 从集合中移除指定的元素 |
| void clear() | 清空集合中的元素 |
| boolean contains(Object o) | 判断集合中是否存在指定的元素 |
| boolean isEmpty() | 判断集合是否为空 |
| int size() | 集合的长度,也就是集合中元素的个数 |
实例:
/*
boolean add(E e) 添加元素
boolean remove(Object o) 从集合中移除指定的元素
void clear() 清空集合中的元素
boolean contains(Object o) 判断集合中是否存在指定的元素
boolean isEmpty() 判断集合是否为空
int size() 集合的长度,也就是集合中元素的个数
*/
public class CollectionTest2 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
c.add("hello,world!");
c.add("hello,java!");
c.add("hello,java!");
System.out.println(c);
System.out.println(c.remove("hello,world!"));
System.out.println(c);
c.clear();
System.out.println(c);
c.add("hello,world!");
c.add("hello,java!");
c.add("hello,java!");
System.out.println(c.contains("hello,world!"));
System.out.println(c.isEmpty());
}
}
控制台输出:

1.5 Conlection集合的遍历
Iterator:迭代器,集合的专用遍历模式
Java Iterator不是一个集合,它是一种用于访问集合的方法,可用于迭代ArrayList和HashSet等集合。
- Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
- 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Interator中的常用方法
- E next():返回迭代中的下一个元素,并且更新迭代器的状态
- boolean hasNext():如果迭代具有更多元素,则返回true
-
public void remove():将迭代器返回的元素删除
实例:
public class InteratorTest {
public static void main(String[] args) {
//创建集合对象
Collection<String> c = new ArrayList<>();
//添加元素
c.add("hello,world!");
c.add("hello,java!");
c.add("hello,python!");
//获取迭代器 Iterator<E> iterator(),返回此集合中元素的迭代器,通过集合的iterator()方法得到
Iterator<String> it = c.iterator();
/*
* public Iterator<E> iterator()
* private class Itr implements Iterator<E>
*/
System.out.println(c);
/*System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());*/
/*System.out.println(it.next());//Exception in thread "main" java.util.NoSuchElementException:表示被请求元素不存在*/
/*if(it.hasNext()) {
System.out.println(it.next());
}*/
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
控制台输出:

1.6 集合的使用步骤
实例:
public class InteratorTest {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("hello,world!");
c.add("hello,java!");
c.add("hello,python!");
Iterator<String> it = c.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}

2. List
2.1 List集合概述和特点
List集合概述
- 有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素。
- 与Set集合不同,列表通常允许重复的元素
List集合特点
- 有序:存储和取出的元素顺序一致
- 可重复:存储的元素可以重复
实例:
public class ListTest {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<String>();
//添加元素
list.add("hello,world!");
list.add("hello,java!");
list.add("hello,python!");
//输出集合对象
System.out.println(list);
//迭代器遍历List
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
控制台输出:

2.2 List集合特有方法
| 方法名 | 说明 |
| 在此集合中的指定位置插入指定的元素 |
| 删除指定索引处的元素,返回被删除的元素 |
| 修改指定索引处的元素,返回被修改的元素 |
| 返回指定索引出的元素 |
List的特有方法,Collection是没有的,但是子类ArrayList是有的
实例:
public class ListTest2 {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<String>();
//添加元素
list.add("hello,world");
list.add("hello,python");
list.add("hello,java");
//void add(int index, E element);
System.out.println("添加前:" + list);
list.add(1,"hello,c++");
/*list.add(11,"hello,javascript");//java.lang.IndexOutOfBoundsException:索引越界*/
//输出集合对象
System.out.println("添加后:" + list);
//E remove(int index) 删除指定索引处的元素,返回被删除的元素
System.out.println("删除的元素为:" + list.remove(1) + " 删除后的列表为:" + list);
//E set(int index, E element) 修改指定索引处的元素,返回被修改的元素
System.out.println("修改的元素为:" + list.set(1,"hello,html") + " 修改后的列表为:" + list);
//E get(int index) 返回指定索引出的元素
System.out.println("获取到的元素为:" + list.get(1));
//遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
控制台输出:

2.3 并发修改异常
案例:
public class ListTest3 {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<String>();
//添加元素
list.add("hello,world");
list.add("hello,python");
list.add("hello,java");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if(s.equals("hello,java")) {
list.add("javaee");
}
}
System.out.println(list);
}
}
控制台输出:

出现原因:分析源码
迭代器遍历过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断预期修改值和实际修改值不一致
public interface List<E> extends Collection<E> {
Iterator<E> iterator();
boolean add(E e);
}
public abstract class AbstractList<E> {
protected int modCount = 0; //protected修饰的变量儿子可以继承
}
public class ArrayList<E> extends AbstractList<E> implements List<E> {
public E get(int index) {
Objects.checkIndex(index, size);
return elementData(index);
}
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int expectedModCount = modCount;
/*
modCount:实际修改集合的次数
expectedModCount:预期修改集合的次数
*/
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
}
修改方法:使用for循环,然后用集合对象做对应的操作即可
public class ListTest3 {
public static void main(String[] args) {
//创建集合对象
List<String> list = new ArrayList<String>();
//添加元素
list.add("hello,world");
list.add("hello,python");
list.add("hello,java");
Iterator<String> it = list.iterator();
/*while (it.hasNext()) {
String s = it.next();
if(s.equals("hello,java")) {
list.add("javaee");
}
} */
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if(s.equals("hello,java")) {
list.add("javaee");
}
}
System.out.println(list);
}
}
控制台输出:

这个案例告诉我们在使用迭代器时不要修改集合的长度!
2.4 ListIterator
ListIterator:列表迭代器
- 通过List集合的listIterator()方法得到,所以说它是List集合特有的迭代器
- 用于循序程序员沿任一方向遍历列表的列表迭代器,在迭代器间修改列表,并获取列表中迭代器当前位置
ListIterator中的常用办法
- E next(): 返回迭代中的下一个元素
- boolean hasNext(): 如果迭代具有更多元素,则返回true
- E previous(): 返回列表中的上一个元素
- boolean hasPrevious(): 如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true
实例:
public class ListIteratorTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
//添加元素
list.add("hello,world");
list.add("hello,java");
list.add("hello,python");
//通过List集合的listIterator()方法获得
/*ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
System.out.println("======================");
while (it.hasPrevious()) {
String it2 = it.previous();
System.out.println(it2);
}*/
//获取列表迭代器
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
String s = it.next();
if(s.equals("hello,java")) {
it.add("javaee");
}
}
System.out.println(list);
}
}
控制台输出:

源码分析:
public interface List<E> extends Collection<E> {
Iterator<E> iterator();
ListIterator<E> listIterator();
}
public abstract class AbstractList<E> {
protected int modCount = 0; //protected修饰的变量儿子可以继承
}
public class ArrayList<E> extends AbstractList<E> implements List<E> {
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
}
public ListIterator<E> listIterator() {
return new ListItr(0);
}
private class ListItr extends Itr implements ListIterator<E> {
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
}
我们可以看到在此add方法中会将实际修改值赋值给预期修改值

2.5 增强for循环
增强for:简化数组和Collection集合的遍历
- 实现Iterator接口的类允许其对象成为增强型for语句的目标
- 他是jdk5之后出现的,其内部原理是一个iterator迭代器
增强for的格式
for(元素数据类型变量名;数组或者Collection集合) {
//在此处使用变量即可,该变量就是元素
}
实例:
public class forTest {
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
for (int i :
array) {
System.out.println(i);
}
String[] strArray = {"hello,world", "hello,java", "hello,python"};
for (String s :
strArray) {
System.out.println(s);
}
System.out.println("==========");
List<String> list = new ArrayList<>();
list.add("hello,world!!!");
list.add("hello,java!!!");
list.add("hello,python!!!");
for (String s :
list) {
System.out.println(s);
}
System.out.println("==========");
//判断内部实现是不是迭代器
for (String s :
list) {
if (s.equals("hello,world!!!")) {
list.add("javaee");
}
}
}
}
控制台输出:

报错说明出现并发修改异常,内部为迭代器
2.6 List集合子类特点
List集合常用子类:
- ArrayList:底层数据结构是数组,查询快,增删慢
- LinkedList:底层数据结构是链表,查询慢,增删快
2.7 LinkedList集合的特有功能
| 方法名 | 说明 |
| 在该列表开头插入指定的元素 |
| 将指定的元素追加到此列表的末尾 |
| 返回此列表中的第一个元素 |
| 返回此列表中的最后一个元素 |
| 从此列表中删除并返回第一个元素 |
| 从此列表中删除并返回最后一个元素 |
实例:
/*
方法名 说明
public void addFirst(E e) 在该列表开头插入指定的元素
public void addLast(E e) 将指定的元素追加到此列表的末尾
public E getFirst() 返回此列表中的第一个元素
public E getLast() 返回此列表中的最后一个元素
public E removeFirst() 从此列表中删除并返回第一个元素
public E removeLast() 从此列表中删除并返回最后一个元素
*/
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("hello,world!!!");
list.add("hello,java!!!");
list.add("hello,python!!!");
list.addFirst("javee!");
list.addLast("javase!");
System.out.println(list);
System.out.println(list.getFirst());
System.out.println(list.getLast());
System.out.println(list.removeFirst());
System.out.println(list.removeLast());
System.out.println(list);
}
}
控制台输出:

3466




被折叠的 条评论
为什么被折叠?



