目录
1. 集合的概念
-
概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能
-
和数组的区别:
- 数组长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
-
位置:java. until. *
2. Collection体系
- Interface Collection(该体系的根接口,代表一组对象,称为“集合”)
- Interface List(有序、有下标、元素可重复)
- Class ArrayList
- Class LinkedList
- Class Vector
- Interface Set(无序、无下标、元素不能重复)
- Class HashSet
- Interface SortedSet
- Class TreeSet
- Interface List(有序、有下标、元素可重复)
3.Collection接口
- 特点:代表一组任意类型的对象,无序、无下标、不能重复
- 方法:
- boolean add(Object obj):添加一个对象
- boolean addAll(Collection c):将一个集合中的所有对象添加到此集合中
- void clear():清空此集合中所有的对象
- boolean contains(Object obj):检查此集合中是否包含obj对象
- boolean equals(Object obj):比较此集合是否与指定对象相等
- boolean isEmpty():判断此集合是否为空
- boolean remove(Object obj):在此集合中移除obj对象
- int size():返回此集合中的元素个数
- Object[] toArray():将此集合转换成数组
4. Collection使用
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test {
public static void main(String[] args) throws Exception {
// 创建集合
Collection collection = new ArrayList();
// 添加元素
collection.add('A');
collection.add('B');
collection.add('C');
System.out.println(collection);
System.out.println(collection.size());
// 删除元素
collection.remove('B');
System.out.println(collection);
// 遍历元素
// 1. 使用增强for
for (Object obj : collection) {
System.out.println(obj);
}
// 2. 使用迭代器
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 判断
System.out.println(collection.contains('A'));
System.out.println(collection.isEmpty());
}
}
5. List接口
- 特点:有序、有下标、元素可以重复
- 方法:
- void add(int index, Object obj):在index位置插入对象obj
- boolean addAll(int index, Collection c):将一个集合中的元素添加到此集合中的index位置
- Object get(int index):返回集合中指定位置的元素
- List subList(int fromIndex, int toIndex):返回fromIndex和toIndex之间的集合元素
6. List使用
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) throws Exception {
// 创建集合对象
List list = new ArrayList<>();
// 添加元素
list.add('A');
list.add('C');
list.add(1, 'B');
list.add(3, 'D');
System.out.println(list);
System.out.println(list.size());
// 删除元素
list.remove((Object) 'B');
list.remove(1);
System.out.println(list);
// 遍历
// 1. 使用for遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 2. 使用增强for
for (Object obj : list) {
System.out.println(obj);
}
// 3. 使用迭代器
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 4.使用列表迭代器
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.nextIndex());
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previousIndex());
System.out.println(listIterator.previous());
}
// 判断
System.out.println(list.contains('D'));
System.out.println(list.isEmpty());
System.out.println(list.indexOf('A'));
}
}
7. List实现类
- ArrayList:
- 数组结构实现,查询快、增删慢
- JDK1.2版本,运行效率快、线程不安全
- Vector:
- 数组结构实现,查询快、增删慢
- JDK1.0版本,运行效率慢、线程安全
- LinkedList:
- 链表结构实现,增删快、查询慢
8. ArrayList使用
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
// 创建集合
ArrayList arrayList = new ArrayList();
// 添加元素
Student student1 = new Student("A", 10);
Student student2 = new Student("B", 20);
Student student3 = new Student("C", 30);
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
arrayList.add(student3);
System.out.println(arrayList.size());
System.out.println(arrayList);
// 删除元素
arrayList.remove(student3);
arrayList.remove(new Student("C", 30));
System.out.println(arrayList);
// 遍历
// 1. 使用迭代器
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 2. 使用列表迭代器
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
// 判断
System.out.println(arrayList.contains(student2));
System.out.println(arrayList.contains(new Student("B", 20)));
System.out.println(arrayList.isEmpty());
System.out.println(arrayList.indexOf(student2));
// 查找
}
}
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
}
9. Vector使用
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
public class Test {
public static void main(String[] args) throws Exception {
// 创建集合
Vector vector = new Vector();
// 添加元素
vector.add('A');
vector.add('B');
vector.add('C');
vector.add('D');
System.out.println(vector);
System.out.println(vector.size());
// 删除元素
vector.remove((Object) 'B');
vector.remove(1);
System.out.println(vector);
// 遍历元素
// 1. 使用for遍历
for (int i = 0; i < vector.size(); i++) {
System.out.println(vector.get(i));
}
// 2. 使用增强for遍历
for (Object obj : vector) {
System.out.println(obj);
}
// 3. 使用迭代器遍历
Iterator iterator = vector.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 4. 使用列表迭代器
ListIterator listIterator = vector.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
// 5. 使用枚举器
Enumeration enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
// 判断
System.out.println(vector.contains('D'));
System.out.println(vector.isEmpty());
// 获取元素
System.out.println(vector.firstElement());
System.out.println(vector.lastElement());
System.out.println(vector.elementAt(0));
System.out.println(vector.get(1));
}
}
10. LinkedList使用
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Objects;
public class Test {
public static void main(String[] args) throws Exception {
// 创建集合
LinkedList linkedList = new LinkedList();
// 添加元素
Student student1 = new Student("A", 10);
Student student2 = new Student("B", 20);
Student student3 = new Student("C", 30);
linkedList.add(student1);
linkedList.add(student2);
linkedList.add(student3);
linkedList.add(student3);
System.out.println(linkedList);
System.out.println(linkedList.size());
// 删除元素
linkedList.remove(student3);
linkedList.remove(new Student("C", 30));
System.out.println(linkedList);
// 遍历
// 遍历元素
// 1. 使用for遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
// 2. 使用增强for遍历
for (Object obj : linkedList) {
System.out.println(obj);
}
// 3. 使用迭代器遍历
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 4. 使用列表迭代器
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
// 判断
System.out.println(linkedList.contains(student1));
System.out.println(linkedList.isEmpty());
// 获取元素
System.out.println(linkedList.get(0));
System.out.println(linkedList.indexOf(student2));
}
}
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
}
11. ArrayList和LinkedList区别
- 不同结构实现方式
- ArrayList:必须开辟连续空间,查询快、增删慢
- LinkedList:双向链表,查询慢、增删快