一、集合
1、什么是集合?集合是做什么的?
集合是一个“容器”,用来装“对象”。
2、集合与数组有什么不同?
数组相当于一种更基础的数据结构,它是线性,长度是不可变(扩容的话是创建了新数组,原有的数组长度并没有变)。
优点:可以根据[下标]快速的访问到某个位置的元素。
缺点:如果要扩容等操作,程序员需要编写相应的代码,
如果要插入元素,不仅要考虑扩容的问题,还要考虑移动元素问题,这些都是程序干的,
如果要删除元素,虽然不用考虑扩容问题,但是要考虑移动元素问题,这些都是程序干的。
程序员需要做的事非常多,对程序员要求非常高。
集合是一种更抽象的数据结构,它的类型也很多,程序员可选择的余地就更多了。
至于集合的内部(底层)怎么实现的话,对于简单的使用者来说可以不用了解的非常透彻。
甚至不用了解它里面的存储、扩容、删除等实现原理,都可以使用。只要会调用API即可。
优点:一个集合的容器,可以自动实现扩容等操作。集合的类型很多,更丰富。
缺点:要记得类型比较多之后,程序员需要知道每一种集合的特点,好做出选择。
但是如果对于高级程序员来说,或者你想要有更深的技术功底,那么还需要了解集合的底层实现大致原理。==>数据结构。
程序 = 数据结构 + 算法。
3、如何学习集合?
(1)让自己先了解JavaSE的核心类库中提供的集合有哪些类型?特别是上课老师讲过的。
先记类名,分类。
例如:ArrayList,TreeSet等
(2)让自己尽量的熟悉它们的API方法。
例如:add,remove等
(3)让自己尽量的多记它们的特点。
例如:ArrayList:动态数组,元素可以重复,可以根据“索引/下标”来访问。
TreeSet:可以让元素按大小顺序排列的集合,并且元素不能重复。
(4)看它们底层如何实现的?(要求比较高,不要追求一天给明白了,可以放长线)
(5)尝试自己默认集合实现自己的容器。(要求非常高)
4、集合的分类
(1)Collection系列:存储“一组”对象的
例如:存储一组字符串,一组数字,一组学生对象,一组图形对象
常见又可以分为两大类:
List系列:有序的列表,元素可以重复的
Set系列:元素是不可重复的
(2)Map系列:存储“键值对(key,value)”的
例如:key是学号,value是学生对象
key是咱们的学员姓名,value是学员的对象(女朋友或男朋友)的姓名
二、Collection系列
1、(1)java.util.Collection接口
Collection 层次结构 中的“根接口”。Collection 表示一组对象,这些对象也称为 collection 的元素。
一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。
JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。
此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
(2)Collection接口的API(JDK1.8之前的API)
添加
boolean add(Object e):一次添加一个元素。
boolean addAll(Collection c):一次添加多个元素
当前集合(调用addAll方法的集合) = 当前集合 ∪ c集合
查找
boolean contains(Object o) :判断某个元素是否存在
boolean containsAll(Collection c) :判断c集合中的所有元素是否在当前集合中存在。
判断c是否是当前集合的子集。不是判断c是否是当前集合的一个对象。
boolean isEmpty() :是否为空
int size() :元素的总个数
删除
boolean remove(Object o) :一次删除一个元素
boolean removeAll(Collection c) :一次删除多个元素
把当前集合中和c集合相同的元素都删除了。
void clear() :清空当前集合
boolean retainAll(Collection c) :当前集合只留下 它俩的交集元素。
当前集合 = 当前集合 ∩ c集合
遍历
Object[] toArray() :把当前集合中的元素,放到一个数组中,然后返回。
Iterator iterator():返回专用的迭代器对象,用于遍历集合
Iterator接口用于遍历的方法有两个:
(1)boolean hasNext()
(2)Object next()
public class TestCollection {
@Test
public void test14() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
//实现一行打印一个元素
Iterator iterator = c1.iterator(); //返回的是Iterator接口的实现类对象
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void test13() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
//实现一行打印一个元素
Object[] objects = c1.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
}
@Test
public void test12() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.retainAll(c2);
System.out.println(c1);//[hello]
}
@Test
public void test11() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.removeAll(c2);
System.out.println(c1);//[world] 删除了c1中和c2重复的元素
}
@Test
public void test10() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
c1.remove("hello");//依赖于元素的equals方法
System.out.println(c1);//[world]
}
@Test
public void test09() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.add(c2);
System.out.println(c1.size());//3
}
@Test
public void test08() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.addAll(c2);
System.out.println(c1.size());//4
}
@Test
public void test07() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
System.out.println(c1.size());//2
}
@Test
public void test06(){
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.addAll(c2);
System.out.println(c1);//[hello, world, hello, atguigu]
System.out.println(c1.contains("atguigu"));//true
System.out.println(c1.containsAll(c2));//true
}
@Test
public void test05(){
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.add(c2); //把c2当成一个整体,即作为1个对象添加到c1中。 比喻,把一个小箱子整体放到大箱子中。
System.out.println(c1);//[hello, world, [hello, atguigu]]
System.out.println(c1.contains("atguigu"));//false
System.out.println(c1.containsAll(c2));//false 相当于判断 c1中是否有"hello",并且是否有"atguigu",挨个儿元素判断
}
@Test
public void test04() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
System.out.println(c1.contains("hello"));//true 调用元素类型的equals方法,例如这里元素是String类型,那么就调用String类的equals方法
System.out.println(c1.contains("java"));//false
}
@Test
public void test03(){
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.add(c2); //把c2当成一个整体,即作为1个对象添加到c1中。 比喻,把一个小箱子整体放到大箱子中。
System.out.println(c1);//[hello, world, [hello, atguigu]]
}
@Test
public void test02(){
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
Collection c2 = new ArrayList();
c2.add("hello");
c2.add("atguigu");
c1.addAll(c2);
System.out.println(c1);//[hello, world, hello, atguigu]
}
@Test
public void test01(){
// Collection coll = new Collection();//不能直接创建接口的对象
// Collection coll = new 实现类();//Collection没有直接的实现类,有更具体的子接口(如 Set 和 List)实现
Collection coll = new ArrayList();//ArrayList是List子接口的实现类,也把它叫做Collection的是实现类。
//左边为啥不写ArrayList?即为什么不写 ArrayList coll = new ArrayList();
//这里因为我们在学习Collection的API,我们想要关注Collection的API,
// 多态引用时,编译时按Collection处理,即这样就只能看到Collection的API。
coll.add("hello");
coll.add("java");
System.out.println(coll); //[hello, java]
}
}
2、Collection系列的集合的遍历
(1)Object[] toArray() :把当前集合中的元素,放到一个数组中,然后返回。
(2)Iterator iterator():返回专用的迭代器对象,用于遍历集合
(3)foreach遍历
for(元素类型 元素名 : 容器名){
}
foreach增强for循环,可以用于遍历 Java中所有数组,Collection系列的集合。
3、Collection集合的元素删除?
(1)Collection集合中是有删除元素的方法
boolean remove(Object o) :一次删除一个元素
boolean removeAll(Collection c) :一次删除多个元素
(2)Collection集合支持的迭代器Iterator中也有remove()
或者换个方式问?为什么集合中已经有remove方法了,迭代器Iterator还要提供remove()方法。
因为集合的remove方法,只能明确删除具体元素,无法根据某个条件删除元素。
需要根据条件删除元素,只能用迭代器Iterator的remove()方法。
结论:(1)在用迭代器Iterator或foreach遍历集合的过程中,需要删除元素的话,不能调用集合的remove方法,会导致ConcurrentModificationException 并发修改异常,或者漏掉某些元素的判断和删除。
只能用迭代器Iterator的remove方法删除。
(2)foreach遍历集合过程中,只能看,不能删除元素。
(3)如果明确要删除的元素对象,直接集合.remove()方法最快
例如:coll集合 [1,2,3,4,5]中删除3,直接删除更快更直接 ,coll.remove(3)
public class TestCollectionRemove {
@Test
public void test05(){
Collection coll = new ArrayList();
//随机产生5个100以内的整数放到coll集合中
Random random = new Random();
for (int i=1; i<=5; i++){
coll.add(random.nextInt(100));
}
System.out.println(coll);//例如:[21, 22, 82, 60, 4]
//需求:删除集合中的偶数
//此时无法直接调用Collection接口的remove方法删除元素
//只能在遍历时删除元素
for(Object obj : coll){
// Object element = iterator.next();//如果用Object变量接收,下面 element % 2==0就会报错,因为Object类型对象无法支持运算符%
Integer element = (Integer) obj;//需要向下转型 , 元素被添加到集合时,被向上转型为Object了,现在向下转回来
if(element % 2 == 0){
coll.remove(element); //在用foreach遍历集合过程中,调用集合的remove方法
//发现报异常 java.util.ConcurrentModificationException 并发修改异常
//有时候没有报这个异常,但是结果也是不对的
}
}
System.out.println(coll);//[21]
}
@Test
public void test04(){
Collection coll = new ArrayList();
//随机产生5个100以内的整数放到coll集合中
Random random = new Random();
for (int i=1; i<=5; i++){
coll.add(random.nextInt(100));
}
System.out.println(coll);//例如:[21, 22, 82, 60, 4]
//需求:删除集合中的偶数
//此时无法直接调用Collection接口的remove方法删除元素
//只能在遍历时删除元素
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
// Object element = iterator.next();//如果用Object变量接收,下面 element % 2==0就会报错,因为Object类型对象无法支持运算符%
Integer element = (Integer) iterator.next();//需要向下转型 , 元素被添加到集合时,被向上转型为Object了,现在向下转回来
if(element % 2 == 0){
coll.remove(element); //在用Iterator遍历集合过程中,调用集合的remove方法
//发现报异常 java.util.ConcurrentModificationException 并发修改异常
//有时候没有报这个异常,但是结果也是不对的
}
}
System.out.println(coll);//[21]
}
@Test
public void test03(){
Collection coll = new ArrayList();
//随机产生5个100以内的整数放到coll集合中
Random random = new Random();
for (int i=1; i<=5; i++){
coll.add(random.nextInt(100));
}
System.out.println(coll);//例如:[21, 22, 82, 60, 4]
//需求:删除集合中的偶数
//此时无法直接调用Collection接口的remove方法删除元素
//只能在遍历时删除元素
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
// Object element = iterator.next();//如果用Object变量接收,下面 element % 2==0就会报错,因为Object类型对象无法支持运算符%
Integer element = (Integer) iterator.next();//需要向下转型 , 元素被添加到集合时,被向上转型为Object了,现在向下转回来
if(element % 2 == 0){
iterator.remove();
}
}
System.out.println(coll);//[21]
}
@Test
public void test02(){
Collection coll = new ArrayList();
coll.add(1);//Integer对象
coll.add(2);
coll.add(3);
coll.add(4);
coll.add(5);
//需求:删除“3”这个元素
coll.remove(3);
System.out.println(coll);//[1, 2, 4, 5]
}
@Test
public void test01(){
/*
集合只能装对象,当我们把基本数据类型的值放到集合中时,会自动装箱为包装类对象。
*/
Collection coll = new ArrayList();
coll.add(1);//Integer对象
coll.add(2);
coll.add(3);
coll.add(4);
coll.add(5);
System.out.println(coll);
}
}
三、java.lang.Iterable接口
foreach其实本质上是依赖于Iterator迭代器来遍历容器的。
在JDK1.5之后,Java开始支持foreach循环,因为Java引入了java.lang.Iterable接口,
凡是实现/继承了这个接口的容器类型(例如:Collection系列的集合)就支持foreach循环。
java.lang.Iterable接口:API中只有一句话:实现这个接口允许对象成为 "foreach" 语句的目标。
java.lang.Iterable接口:有一个抽象方法 Iterator iterator()
换句话说,用foreach进行遍历,其实就是用集合的 Iterator iterator()返回的迭代器对象在遍历集合。
数组类型是编译器自动帮我们实现Iterable接口,所以支持foreach循环。
如果我们将来自己要设计一个新的集合(容器),也可以实现Iterable接口,然后使用foreach遍历。
1、对比Iterable接口和Iterator接口
java.lang.Iterable接口:
抽象方法: Iterator iterator()
java.util.Iterator接口:
抽象方法:boolean hasNext()
抽象方法:Object next()
抽象方法:void remove(),JDK1.8之后改为默认方法
java.lang.Iterable接口,子接口:Collection接口,凡是实现了Collection接口的,就会实现Iterable接口。
例如:ArrayList类实现了List接口,List接口继承了Collection接口,Collection接口又继承了Iterable接口,
所以就相当于ArrayList类实现了Iterable接口,支持foreach循环,实现了Iterable接口的抽象方法Iterator iterator()。
java.util.Iterator接口:它的实现类在哪里呢?
例如:ArrayList类中的是这样实现这个抽象方法Iterator iterator()的:
public Iterator<E> iterator() {
return new Itr(); //Itr类型是实现了Iterator接口的一个实现类。 多态返回值(返回值类型是父类或父接口类型,返回值对象是子类或实现类的对象)
}
我们发现在ArrayList类中有一个“内部类”Itr,它实现Iterator接口。
private class Itr implements Iterator<E> {
...
//重写Iterator接口的hasNext()和next()抽象方法。还重写了remove()等。
}
总结:Iterable接口是集合实现的接口
Iterator接口是集合中“内部类”实现类接口
英语:able结尾,表示可xx的,这里表示可迭代的,可遍历的。
or或er结尾,表示名词,这里表示迭代器,是一种遍历工具。
2、问?为什么要在集合中用“内部类”的形式实现 Iterator接口?
(1)内部类可以直接访问外部类的成员,包括私有的。
像ArrayList等集合,把元素放到内部时,是私有化的。外部只能通过“方法”操作元素,是不能直接访问元素。
所以,如果把迭代器定义在集合类型外面的话,就没法直接访问集合的元素了。
(2)每一种集合的内部实现都不同,例如有动态数组,有链表,有队列等实现,
每一种容器因为底层实现的不同,遍历元素的方式也不同。没法给出一个统一的实现。
即无法用一个类代表所有集合的迭代器。
所以只能每一个集合单独设计它的迭代器。
(3)把迭代器用内部类形式表示,可以对外隐藏细节,统一使用Iterator接口的方法来使用迭代器。
你甚至可以不知道它的具体的内部类是什么。
这里体现了“高内聚,低耦合”的开发原则。
高内聚:内部类
低耦合:统一用Iterator接口
public class TestIterate {
@Test
public void test03(){
//ArrayList是Collection系列的一个集合
ArrayList list = new ArrayList();
Iterator iterator = list.iterator();//这里返回的是 ArrayList中的内部类Itr 的对象,
//Itr是Iterator接口的一个实现类
//这里是一个多态引用,左边是父接口 Iterator类型,右边是实现类Itr的对象。
while(iterator.hasNext()){//运行时,执行的是Itr实现类的hasNext()
System.out.println(iterator.next());//运行时执行的是Itr实现类的next()
}
}
@Test
public void test02(){
int[] arr = {1,2,3,4};
/*
int:元素类型
num:元素名,临时取的名字
arr:数组名
在foreach循环中,每一次循环,num代表其中一个元素,这里没有下标信息
*/
for(int num : arr){
System.out.println(num);
}
}
@Test
public void test01(){
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
//Collection系列的集合,在没有明确指定元素的类型(通过泛型,后面学习)时,就按照Object处理
//元素名,自己取名,就是一个临时名称
//foreach增强for循环是没有下标的概念
for(Object obj : c1){
//obj在foreach循环中,每一次代表一个元素,c1中有几个元素,循环就执行几次
System.out.println(obj);
}
}
@Test
public void test13() {
Collection c1 = new ArrayList();
c1.add("hello");
c1.add("world");
//实现一行打印一个元素
Object[] objects = c1.toArray();
//普通for循环,遍历数组是有下标的概念
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
}
}
四、List接口
1、集合的List接口是在java.util包
java.awt.List是做图形界面时的下拉列表用的。
2、List系列的集合是有序的,可重复的。
List系列的集合的元素访问,可以通过“索引/下标”的方式访问。
就算这个集合的底层不是数组结构实现的,也依然提供通过“索引/下标”的方式访问的方法。
例如:ArrayList底层是动态数组,可以通过“索引/下标”的方式访问
LinkedList底层是链表,依然可以通过“索引/下标”的方式访问
当然集合的元素访问不是通过 集合名[下标]的方式,而是通过调用方法,形参有(int index)下标。
3、List接口是Collection接口的子接口。
即List接口是继承Collection接口的。
换句话说,Collection接口的所有方法,都适用于List接口。
但是List接口又扩展了Collection接口没有的一些方法,就是和“下标”访问方式有关的。
添加
boolean add(Object e):一次添加一个元素。在列表/序列的最后添加
boolean addAll(Collection c):一次添加多个元素,在列表/序列的最后添加
当前集合(调用addAll方法的集合) = 当前集合 ∪ c集合
void add(int index, Object element):一次添加一个元素。在[index]位置添加。
boolean addAll(int index, Collection c) :一次添加多个元素,在列表/序列的[index]位置添加
如果指定的[index]的位置不是最后一个元素的后面,那么就相当于“插入”操作。
查找
boolean contains(Object o) :判断某个元素是否存在
boolean containsAll(Collection c) :判断c集合中的所有元素是否在当前集合中存在。
判断c是否是当前集合的子集。不是判断c是否是当前集合的一个对象。
boolean isEmpty() :是否为空
int size() :元素的总个数
Object get(int index):返回[index]位置的元素
int indexOf(Object o) :查找目标对象o在当前集合中第一次的下标
int lastIndexOf(Object o) :查找目标对象o在当前集合中最后一次的下标
List<E> subList(int fromIndex, int toIndex) :返回[fromIndex,toIndex)范围的子集
删除
boolean remove(Object o) :一次删除一个元素
boolean removeAll(Collection c) :一次删除多个元素
把当前集合中和c集合相同的元素都删除了。
void clear() :清空当前集合
boolean retainAll(Collection c) :当前集合只留下 它俩的交集元素。
当前集合 = 当前集合 ∩ c集合
Object remove(int index) :删除[index]位置的元素
修改:
Object set(int index, Object element) :List新增的方法,用element替换[index]位置的元素,并返回[index]位置元素的元素
遍历
Object[] toArray() :把当前集合中的元素,放到一个数组中,然后返回。
Iterator iterator():返回专用的迭代器对象,用于遍历集合
foreach遍历:
ListIterator listIterator() :返回列表迭代器对象,用于遍历List集合
ListIterator listIterator(int index):返回列表迭代器对象,用于遍历List集合,从指定[index]位置开始遍历
ListIterator支持从前往后,也支持从后往前遍历
ListIterator是Iterator的子接口,包含的方法有:
(1)boolean hasNext()
(2)Object next()
(3)void remove()
(4)boolean hasPrevious()
(5)Object previous()
(6)int nextIndex()
(7)int previousIndex()
(8) void set(E e)
(9))void add(E e)
public class TestList {
@Test
public void test11() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
//遍历过程中,想要在"java"之前添加一个“atguigu"
//从后往前遍历
ListIterator listIterator = list.listIterator(list.size());
while(listIterator.hasPrevious()){//在循环体中只能调用listIterator.previous()一次,不能出现调用2次或更多次,是有问题的
Object element = listIterator.previous();
if("java".equals(element)){
listIterator.add("atguigu");
}
}
System.out.println(list);
}
@Test
public void test10() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
//使用列表迭代器ListIterator遍历List
//从前往后遍历
ListIterator listIterator = list.listIterator(3);
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
}
@Test
public void test09() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
//使用列表迭代器ListIterator遍历List
//从后往前遍历
ListIterator listIterator = list.listIterator(3);
while(listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
}
@Test
public void test08() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
//使用列表迭代器ListIterator遍历List
//从后往前遍历
ListIterator listIterator = list.listIterator(list.size());
while(listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
}
@Test
public void test07() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
//使用列表迭代器ListIterator遍历List
//从前往后遍历
ListIterator listIterator = list.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
}
@Test
public void test06() {
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
Object obj = list.set(1, "atguigu");
System.out.println(obj);
System.out.println(list);
}
@Test
public void test05() {
List list = new ArrayList();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
// list.remove(20);//java.lang.IndexOutOfBoundsException: Index: 20, Size: 4
list.remove(Integer.valueOf(20));
System.out.println(list);
}
@Test
public void test04(){
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
list.remove(2);
System.out.println(list);//[hello, world, mysql, world]
list.remove("mysql");
System.out.println(list);
}
@Test
public void test03(){
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add("java");
list.add("mysql");
list.add("world");
System.out.println(list.get(1));//world
System.out.println(list.indexOf("world"));//1
System.out.println(list.lastIndexOf("world"));//4
List subList = list.subList(1, 3); //[1,3)位置的元素
System.out.println(subList);//[world, java]
}
@Test
public void test02(){
//这里左边写List,也是为了关注List接口的方法
List list1 = new ArrayList();
list1.add("hello");
list1.add("world");
List list2 = new ArrayList();
list2.add("java");
list2.add("mysql");
list1.addAll(1, list2);
System.out.println(list1); //[hello, java, mysql, world]
}
@Test
public void test01(){
//这里左边写List,也是为了关注List接口的方法
List list = new ArrayList();
list.add("hello");
list.add("world");
list.add(0, "java");
System.out.println(list);//[java, hello, world]
}
}
4、List接口的实现类
(1)ArrayList(使用频率最高):动态数组
(2)Vector:动态数组
(3)LinkedList:链表
(4)Stack:栈
早期的时候,是没有集合框架的概念的,JDK1.0的时候只是提供了几个容器类型的抽象实现。
例如:Vector,Hashtable,Stack等。
当时没有考虑到要设计n种集合。
一经推出Java语言非常受欢迎,不同的用户在使用Java语言开发自己的系统/项目时,对容器的需求不同,更多了。
JDK1.2才引入了更多的集合,当类型一下子增多之后,对于程序员来说学习成本增加了,对于JDK开发团队来说,维护成本增加了,
所以他们为了解决这样的问题,就提出“标准”概念,即我们说的接口。
把ArrayList,LinkList,Vector,Stack这些的共同行为特征提取为List接口,
把HashSet,TreeSet等这些的共同行为特征提取为Set接口。
它List接口和Set接口的共同行为特征提取为Collection接口。
就形成了著名的集合框架。
问题1:ArrayList和Vector的区别:
Vector:是最古老的动态数组。比Collection、List接口都早。
线程安全的,适用于多线程,效率有所下降。
当内部的数组容量不够时,如果用户没有手动指定扩容数量的话,会自动扩容为原来的2倍。
假设:数组的初始容量是10,陆续添加的元素达到81
2倍:
优点: 减少扩容的次数。 10->20->40->80->160
缺点:可能造成空间的浪费的可能更大。
1.5倍:
优点:空间利用率相对较高
缺点:扩容的频率会增加。 10->15->22-->33->49->73->109
扩容频率增加的话,会导致复制数组的次数增多,影响性能,要回收的旧数组也多。
ArrayList:是后加集合类型。
线程不安全,适用于单线程,效率更高。
当内部的数组容量不够时,如果用户没有手动指定扩容数量的话,会自动扩容为原来的1.5倍。
回忆:
StringBuffer:古老,线程安全的,适用于多线程,效率有所下降。
StringBuilder:JDK1.5加,线程不安全的,适用于单线程,效率更高。
问题2:Stack和 动态数组(ArrayList、Vector)的区别?
Stack是Vector的子类,它是为了体现“先进后出(FILO),或者说后进先出(LIFO)”的数据结构而设计的一种集合。
F:first,L: last,I:in , O:out
这里相当于用动态数组来实现栈结构。
为了区别于普通的动态数组,在Stack类中增加了几个方法:
(1)peek():看栈顶元素
(2)pop():弹走栈顶元素
(3)push():把新元素压入栈称为新的栈顶元素
栈结构可以想象成“纸箱子”,先放进去的在“栈底”,后放进去的在“栈顶”
(4)boolean empty():栈是否为空
(5)int search(Object o):返回对象在堆栈中的位置,以 1 为基数。
问题3:LinkedList和动态数组的区别?
LinkedList:底层实现是链表,而且是双向链表。
(1)每一个元素需要用一个“结点”包装,包含(前一个元素的引用previous,中间的数据(真正的元素内容)、后一个元素的引用next)
(2)元素不要求挨着,我们是通过记录前后元素的“首地址(即引用)”来找到前后元素。
缺点:如果根据“索引”来存或取元素的话,效率不高。需要现统计索引。
优点:当我们需要在中间插入时,不需要移动元素,也不需要考虑扩容问题。
当我们需要在中间删除时,不需要移动元素。
当我们需要在链表的默认插入和删除元素,非常快,找到last结点,直接添加或删除即可。
动态数组(ArrayList、Vector):底层实现是数组。
(1)每一个元素是相邻的存储,整个集合(动态数组)开辟一整块连续的存储空间。
优点:如果根据“索引”来存或取元素的话,效率是很高。
缺点:当我们需要在中间插入时,需要移动元素,也需要考虑扩容问题。
当我们需要在中间删除时,需要移动元素。
当我们需要删除数组的最后一个元素,也是很快的,不需要移动元素。
当我们需要最后一个元素后面添加新元素,只需要考虑是否扩容,也不用移动元素。
如何选择它们? LinkedList还是 ArrayList?
如果你添加元素都是末尾添加和删除的话,它们区别不是特别大。
如果你需要在中间添加和删除元素的话,而且比较频繁,还是考虑 LinkedList好一点。
如果你大多数操作都是需要根据"索引“的话,那么考虑 ”ArrayList“。
public class TestListImpl {
@Test
public void test07() {
//分析底层实现的内存效果
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
}
@Test
public void test06() {
//分析底层实现的内存效果
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
}
@Test
public void test05() {
//演示push和indexOf和search方法
Stack stack = new Stack();
stack.push("hello");
stack.push("world");
stack.push("java");
System.out.println(stack.indexOf("world"));//1 下标,从0开始
System.out.println(stack.search("world"));//2 顺序,第几个,从1开始
}
@Test
public void test04(){
//演示push和pop方法,empty的配对使用,用循环弹出栈顶元素
Stack stack = new Stack();
stack.push("hello");
stack.push("world");
stack.push("hello");
stack.push("java");
stack.push("world");
stack.push("atguigu");
System.out.println(stack);//[hello, world] 直接打印看不出来栈结构,因为toString体现的是动态数组的统一特性
while(!stack.empty()){
System.out.println(stack.pop());
}
}
@Test
public void test03() {
//演示把Stack当成普通的动态数组使用
Stack stack = new Stack();
stack.add("hello");
stack.add("java");
stack.add("world");
stack.add("atguigu");
System.out.println(stack.get(0));
stack.remove("java");
//foreach循环的快捷键iter
for (Object o : stack) {
System.out.println(o);
}
}
@Test
public void test02() {
//演示push和peek方法的配对使用
Stack stack = new Stack();
stack.push("hello");
stack.push("world");
System.out.println(stack.peek());//world
System.out.println(stack.peek());//world
System.out.println(stack.peek());//world
System.out.println(stack.peek());//world
System.out.println(stack.peek());//world
}
@Test
public void test01(){
//演示push和pop方法的配对使用
Stack stack = new Stack();
stack.push("hello");
stack.push("world");
System.out.println(stack);//[hello, world] 直接打印看不出来栈结构,因为toString体现的是动态数组的统一特性
System.out.println(stack.pop());//world
System.out.println(stack.pop());//hello
System.out.println(stack.pop());//报异常EmptyStackException
}
}