集合框架
目标
- 集合的概念
- Collection接口
- List接口与实现类
- 泛型和工具类
- Set接口与实现类
- map接口与实现类
什么是集合
概念:对象的容器,定义了多个对象进行操作的常用方法。可实现数组的功能
和数组的区别:
- 数组长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能存储引用类型
位置:java.util.*;
Collection体系集合
List接口的特点:有序,有下表,元素可重复
Set接口的特点:无序,无下标,元素不能重复
Collection父接口
特点:代表一组任意类型的对象,无序,无下标,不能重复
方法:
boolean add(Object obj) //添加一个对象
boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中。
void clear() //清空此集合的所有对象
boolean contains(Object o) //比较此集合是否包含o对象
boolean equals(Object o) //比较此集合是否与指定对象相等
boolean isEmpty() //判断此集合是否为空
boolean remove(Object o) //在此集合中移除o对象
int size() //返回此集合中的元素个数
Object[] toArray() //将此集合转换成数组
Collection使用,Integer
(1)
增强for,遍历,foreach
package J;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
//1.添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//2.删除元素
// collection.remove("榴莲");
// System.out.println("删除之后:"+collection.size());
// collection.clear();
//3.遍历元素【重点】
//3.1使用增强for
for (Object object : collection) {
System.out.println(object);
}
System.out.println("=========================");
//3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
//hasNext();判断有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
Iterator it = collection.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//collection.remove(s); ConcurrentModificationException 并发修改异常
//it.remove(); 可以使用 it.remove();
}
System.out.println("元素个数"+collection.size());
//4.判断
System.out.println(collection.contains("西瓜"));
}
}
Iterator
//3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
//hasNext();判断有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
(2)
package J;
//Collection的使用:保存学生信息
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
//创建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("张无忌",18);
Student s3 = new Student("王二",22);
//1.添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//2.删除
collection.remove(s1);
//collection.remove(new Student("王二",22)); 相当于新创建一个又删除一个
collection.clear();//把集合的地址删除,但数据还在堆里
//3遍历
//3.1增强for
for(Object object : collection){
Student s = (Student)object;
System.out.println(s.toString());
}
//3.2迭代器
Iterator it = collection.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}
List集合
List子接口
特点:有序,有下表,元素可重复
方法:
void add(int index,Object o) //在index位置插入对象o
boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
Object get(int index) //返回集合中特定位置的元素
List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素
List子接口的使用
(1)
List list = new ArrayList<>();
package J;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Demo03 {
public static void main(String[] args) {
//创建集合
List list = new ArrayList<>();
//1.添加元素
list.add("苹果");
list.add("小米");
list.add(0,"华为");//带下标,在第一个位置
System.out.println("元素个数:"+list.size());
//2.删除元素
// list.remove(0);
// System.out.println("删除之后:"+list.size());
// System.out.println(list.toString());
//3.遍历
//3.1使用for遍历
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i));
}
System.out.println("=====================");
//3.2增强for
for (Object object : list){
System.out.println(object);
}
System.out.println("=====================");
//3.3使用迭代器
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//3.4使用列表迭代器 ListIterator<E>,和Iterator的区别,可以向前或向后遍历,添加,删除,修改元素
ListIterator lit = list.listIterator();
System.out.println("从前往后");
while(lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
System.out.println("从后往前");
while(lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//4.判断
System.out.println(list.contains("苹果"));
System.out.println(list.isEmpty());
//5.获取位置
System.out.println(list.indexOf("华为"));
}
}
(2)
List实现类
ArrayList【重点】:
数组结构实现,查询快,增删慢;
JDK1.2版本,运行效率快,线程不安全
Vector:
数组结构实现,查询快,增删慢;
JDK1.0版本,运行效率慢,线程安全。
LinkedList:
链表结构实现,增删快,查询慢
ArrayList使用
package J;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Demo05 {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList<>();
//1.添加元素
Student s1 = new Student("刘德华",20);
Student s2 = new Student("郭富城",22);
Student s3 = new Student("梁朝伟",18);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//2.删除元素
// arrayList.remove(new Student("郭富城",22));//重写方法后可实现
// //arrayList.remove(s1);
// System.out.println("删除之后:"+arrayList.size());
//3.遍历数组【重点】
//3.1使用迭代器
Iterator it = arrayList.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.toString());
}
//3.2列表迭代器
ListIterator lit = arrayList.listIterator();
System.out.println("------3.2------");
while(lit.hasNext()){
Student s = (Student)lit.next();
System.out.println(s.toString());
}
System.out.println("------逆序------");
while(lit.hasPrevious()){
Student s = (Student)lit.previous();
System.out.println(s.toString());
}
//判断
System.out.println(arrayList.contains(new Student("梁朝伟",18)));
System.out.println(arrayList.isEmpty());
//5.查找
System.out.println(arrayList.indexOf(new Student("梁朝伟",18)));
}
}
遇到的报错
NullPointerException
原因是这里需要给Student里的元素分配内存
而我忘记在父类Student中创建一个super
super代表父类存储空间的标识,super可以通过这个标识访问父类的成员,super可以理解为父类对象