1 集合的概念
什么是集合?
概念:集合是对象的容器,定义了对多个对象进行操作的常用方法。可以实现数组的功能。
集合与数组的区别:
1、数组的长度固定,集合长度不固定;
2、 数组可以存储基本数据类型和引用数据类型,集合只能存储引用数据类型。
2、Collection接口
Collection体系集合
Collection接口
1、特点:代表一组任意类型的对象。无序、无下标、元素不可重复。
2、常用方法:
添加一个对象:boolean add();
将一个集合中的所有对象添加到此集合中:boolean addAll();
清空此集合中的所有对象:void clear();
检查此集合中是否包含某个对象:boolean contains();
检查此集合中是否包含指定集合中的所有元素:boolean containsAll();
比较此集合中是否有与指定对象相等的对象:boolean equals();
判断此集合是否为空:boolean isEmpty();
移除此集合中的指定对象:boolean remove();
移除此集合中包含指定集合中的所有元素:boolean removeAll();//移除两个集合中的相同元素
保留此集合中也包含在指定集合中的所有元素:boolean retainAll();//两个集合求交集
返回集合中的元素个数:int size();
将集合转换为数组:Object[] toArray();
3、遍历Collection元素:
首先创建Collection集合:
Collection collection = new ArrayList();//创建Collection集合
遍历方法一:因为Collection是无序的,遍历时使用增强型for循环;
for (Object object : collection) {
}
遍历方法二:使用迭代器,迭代器常用方法如下:
1、hasNext():是否有下一元素
2、next():获取下一元素
3、remove():删除当前元素
需要注意的一点是,在遍历迭代器的过程中,不能使用collection的删除方法。不然会抛并发修改异常。
Iterator iterator = collection.iterator();
while (iterator.hasNext()){
String s = (String) iterator.next();
iterator.remove();
}
4、 实例化对象时存储的值相同,也不会影响Collection 的操作,因为操作的是不同的对象。collection.clear()方法删除的是指向对象的地址,实际上对象还是存在的。
Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
Student s4 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s4);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}, Student{name='小明', age=18}]
collection.remove(s3);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
collection.remove(new Student("小明",18));//此方法现不能删除。如果重写Collection的equals方法也可以删除
System.out.println(collection);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
Collection collection = new ArrayList();
Student s1 = new Student("张三",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小明",18);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection.contains(s1));//true
System.out.println(collection.contains(new Student("小明",18)));//false
3、List接口和实现类
3.1 List接口
List接口的特点:有序、有下标、元素可以重复。
List常用方法:
1、添加元素:list.add();
List list = new ArrayList();
//list在进行add时,如果指定下标,则按最新下标依次插入。插入的下标不可越界,否则会抛出异常。
list.add("苹果");
list.add("榴莲");
list.add(0,"荔枝1");
list.add(0,"荔枝2");
list.add("荔枝3");
System.out.println(list.size());//5
System.out.println(list.toString());//[荔枝2, 荔枝1, 苹果, 榴莲, 荔枝3]
索引排列:
2、删除list中的元素,下标要已存在:list.remove();
list.remove(2);//按索引值移除
list.remove("苹果");//删除指定对象
3、重置元素,下标要已存在:list.set();
List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add(0,"荔枝1");
list.set(0, "apple");
System.out.println(list.toString());//[apple, 苹果, 榴莲]
4、遍历有4种方式
4.1普通for循环和增强for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for (Object object : list) {
System.out.println(object);
}
4.2迭代器遍历
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
4.3列表迭代器遍历
List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
System.out.println("使用列表迭代器 从前向后 遍历");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("使用列表迭代器 从后向前 遍历");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
打印结果:
5、判断
List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
//判断元素是否在集合中存在
System.out.println(list.contains("苹果"));//true
//判断集合是否为空
System.out.println(list.isEmpty());//false
6、获取位置
List list = new ArrayList();
list.add("苹果");
list.add("榴莲");
list.add("荔枝1");
System.out.println(list.indexOf("榴莲"));//1
System.out.println(list.indexOf("荔枝2"));//-1:该对象不存在于list集合中
7、List添加和删除数字
是否可以用indexOf,明天测试
List list = new ArrayList();
list.add(20);//list.add()在添加数字是自动装箱操作,int是基本数据类型,list中实际的对象是Integer包装类
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println(list);
//删除数据20
//list.remove(20);//此行代码执行报数组下标越界的异常。当删除数字时,默认是按int类型的索引值进行删 除
//可以将数字转换为其他类型
list.remove((Object)20);
list.remove((Integer)30);
list.remove(new Integer(40));
8、获取list的子集合,包头不包尾:list.subList();
List list = new ArrayList();
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println(list);//[20, 30, 40, 50, 60]
List list2 = list.subList(1, 3);
System.out.println(list2);//[30, 40]
3.2 List实现类
3.2.1 ArrayList
特点:数组结构实现,查询快、增删慢;运行效率快,线程不安全。
1、添加元素
ArrayList arrayList = new ArrayList<>();
Student s1 = new Student("小明",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小刚",18);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println(arrayList);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}, Student{name='小明', age=18}]
arrayList.remove(new Student("小明",18));//重写Collection的equals方法
System.out.println(arrayList);//打印结果:[Student{name='张三', age=18}, Student{name='小红', age=18}]
重写Collection的equals方法:
@Override
public boolean equals(Object obj) {
//判断是否指向的地址相同,相同则存储的是同一个对象
if (this == obj){
return true;
}
//判断是否为空
if (obj == null){
return false;
}
//判断是否是Student类型
if (obj instanceof Student){
if (this.name == ((Student) obj).name && this.age == ((Student) obj).age){
return true;
}
}
return false;
}
2、删除元素
arrayList.remove(s1);//删除对象
arrayList.remove(2);//按下标删除
3、遍历元素
普通for循环和增强型for循环分别进行遍历
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
for (Object object : arrayList) {
System.out.println(object);
}
迭代器遍历进行遍历:
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
列表迭代器遍历,从前向后遍历 、从后向前遍历:
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()){//从前向后遍历
System.out.println(listIterator.next());
}
System.out.println("------------------------");
while (listIterator.hasPrevious()){//从后向前遍历
System.out.println(listIterator.previous());
}
4、判断
System.out.println(arrayList.contains(s1));//true
System.out.println(arrayList.contains(new Student("小刚",18)));//true :前面已重写equals方法,所以结果为true,如果没有重写,则为false
System.out.println(arrayList.isEmpty());//false
5、查找
System.out.println(arrayList.indexOf(new Student("小刚",18)));//2:前面已重写equals方法,所以结果为true,如果没有重写,则为false
System.out.println(arrayList.indexOf(s2));//1
源码分析
DEFAULT_CAPACITY = 10:默认容量。没有向集合添加任何元素时,容量是0。添加第一个元素之后,容量变为10。每次扩容都是原来的1.5倍。
Object[] elementData:存放元素的数组
int size:实际元素个数
3.2.2 Vector
特点:数组结构实现,查询快、增删慢;运行效率慢,线程安全。
1、添加元素
Vector vector = new Vector<>();
vector.add("盐水鸭");
vector.add("鸭血粉丝汤");
vector.add("糖芋苗");
System.out.println(vector);//[盐水鸭, 鸭血粉丝汤, 糖芋苗]
2、删除元素
vector.remove("糖芋苗");
vector.remove(2);
3、遍历元素:使用枚举器进行遍历
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
System.out.println(elements.nextElement());
}
4、判断元素
System.out.println(vector.contains("糖芋苗"));//true
System.out.println(vector.contains("地锅鸡"));//false
System.out.println(vector.isEmpty());//false
3.2.3 LinkedList
特点:链表结构实现,查询慢,增删快。
1、添加元素
//双向链表
LinkedList linkedList = new LinkedList();
Student s1 = new Student("小明",18);
Student s2 = new Student("小红",18);
Student s3 = new Student("小刚",18);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println(linkedList);//[Student{name='小明', age=18}, Student{name='小红', age=18}, Student{name='小刚', age=18}]
2、删除元素
linkedList.remove();//remove方法中没有参数,则默认删除第一个元素
linkedList.remove(0);//根据下标删除
linkedList.remove(s3);//删除指定对象
3、遍历元素
普通for循环和增强型for循环分别进行遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
System.out.println("-------------------");
for (Object object : linkedList) {
System.out.println(object);
}
迭代器遍历进行遍历:
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
列表迭代器遍历,从前向后遍历 、从后向前遍历:
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()){
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
4、判断
System.out.println(linkedList.contains(s1));//true
System.out.println(linkedList.isEmpty());//false
5、获取
System.out.println(linkedList.indexOf(s3));//2
System.out.println(linkedList.indexOf(new Student("小刚",18)));//如果如ArrayList中重写了equals方法,则打印2。如果没有重新,打印结果为-1
4、泛型和工具类
5、Set接口与实现类
6、Map接口与实现类