Java集合 Java随笔记

「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战


相关文章

Java随笔记:随笔记


前言

  • 刚跳槽换了工作,最近忙的焦头烂额,回想起来,还是老东家的日子才是天堂啊!
  • 感觉自己停更蛮久了,but,2021的最后日更活动,咬着牙也要坚持下去啊!hahah
  • 中间件的更新暂且停一停,最近特别想整理下先前面试的过程【痛苦又真实】。
  • 接下来我会更新一些java基础的内容,有兴趣的可以看看,对我本人来说,这是回顾,这是记录。对各位看官来说,也是一个总结吧,希望对各位有帮助哦~
  • 当然,也有水文的因素在里面,我怂我承认~

一、Java集合

  • 集合类存放于 Java.util 包中,主要有 3 种:set(集)、list(列表包含 Queue)和 map(映射)。
    • Collection:Collection 是集合 List、Set、Queue 的最基本的接口。
    • Iterator:迭代器,可以通过迭代器遍历集合中的数据
    • Map:是映射表的基础接口
  • 大体关系如下图:
    • image-20211102225525386.png

二、List

  • 在实际工作中,可能我们用的最多的就是List了吧,无处不在!无所不能!
  • Java 的 List 是非常常用的数据类型。List 是有序的 Collection。Java List 一共三个实现类:分别是 ArrayList、Vector 和 LinkedList。
  • 打开Idea瞅一瞅,
+ ![image-20211102225836179.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/8524254c2aa881fcb033a088e52547255440d822535efa9972126a26888f2c4d)
+ ![image-20211102225901717.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/42cb376205a00890a69b76d14044f050c224cb9357932c988c7662c05aaad627)
+ 和上面的图一直,继承了Collection,再往上就是迭代器Iterable。
  • 那么,List的三种实现有什么区别呢?

三、ArrayList

  • ArrayList : 数组,这个也应该是用的最多的。最常见的。
  • ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中。当从 ArrayList 的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。
  • 还是看看idea中的继承链:
+ ![image-20211102230329285.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/92cf5ed98508fdccb42b8fa7a689b8010760469d061d8d77da52caa3ee8fb090)
+ 蓝色实线条代表继承,绿色虚线条代表实现。
+ 可以很明显的看出,ArrayList 继承了AbstractList,实现了RandomAccess、Cloneable、java.io.Serializable。
+ 点到List源码中看一看:
+ ![image-20211102231012544.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/df0737238fccb1ad03ef38288dba2c2a43f7cafc9b556eb28646fe9e01b55c31)
+ 既然实现了Cloneable接口,那么代表了它覆盖了函数clone(),证明他是可以被克隆的。
+ 既然实现了java.io.Serializable接口,意味着它支持序列化,能通过序列化进行传输。
+ 我们再看一看AbstractList的继承链和源码:
+ ![image-20211102231210709.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/610a1a000f316b7e6d1337446e3f94cebf88b9a2716e67f648143d1fdcf422ae)
+ ![image-20211102231222779.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/66151b992a84669b4929a92ad8b8701d0e59ffdffd208c180668306e5fb6c667)
+ 呀,这玩意怎么也实现了List,那为什么我们ArrayList已经继承了AbstractList还要继续实现List呢?


    - 我们自己模拟一个类似这种的实现来看看(此模拟过程参考了其他博主的文章,鉴于不是掘金的文章,这里不贴链接了。)
    - 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
java复制代码public class Test {

public static interface MyInterface {
void foo();
}

public static class BaseClass implements MyInterface, Cloneable, Serializable {

@Override
public void foo() {
System.out.println("BaseClass.foo");
}
}

public static class Class1 extends BaseClass {

@Override
public void foo() {
super.foo();
System.out.println("Class1.foo");
}
}

static class Class2 extends BaseClass implements MyInterface, Cloneable,
Serializable {

@Override
public void foo() {
super.foo();
System.out.println("Class2.foo");
}
}

public static void main(String[] args) {

showInterfacesFor(BaseClass.class);
showInterfacesFor(Class1.class);
showInterfacesFor(Class2.class);
}

private static void showInterfacesFor(Class<?> clazz) {
System.out.printf("%s --> %s\n", clazz, Arrays.toString(clazz
.getInterfaces()));
}
}
- 输出结果如下 * ![image-20211102231527660.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/b108887ba171221cdac6957f20dd21cf749cbb5b881bc6db8dd122b557028783) - 从结果可以看出虽然Class1类的父类实现了接口,但是本身并没有再次实现接口,因此通过java.lang.Class直接获取Class1类的接口为空数组。 - 因此在实现代理的时候就会出现问题。所以可能是设计者就是特意这样设计的嘞?有特别深入了解者,可以解惑下!
  • 唔,这里就点到为止吧,我不知道这个文章需要详细到什么地步?需要贴具体的add和remove的源码讲解嘛?
  • 因此,它适合随机查找和遍历,不适合插入和删除。

四、Vector

  • Vector 与 ArrayList 一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写 Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费。因此,访问它比访问 ArrayList 慢。
  • 点击去看一看源码
+ ![image-20211104194534818.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/c2c82623bb8ba9f53a52da8b52bf1451063b237bb6ab09803ee788727a87d94c)
+ 继承了AbstractList,实现了List;所以,**它是一个队列,支持相关的添加、删除、修改、遍历等功能**。
+ 实现了RandmoAccess接口,即**提供了随机访问功能**。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。
+ 实现了Cloneable接口,即实现clone()函数。它能被克隆。
  • 我们看下Vector的构造函数:
+ 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
java复制代码/**
* Constructs an empty vector with the specified initial capacity and
* capacity increment.
*
* @param initialCapacity the initial capacity of the vector
* @param capacityIncrement the amount by which the capacity is
* increased when the vector overflows
* @throws IllegalArgumentException if the specified initial capacity
* is negative
capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}

/**
* Constructs an empty vector with the specified initial capacity and
* with its capacity increment equal to zero.
*
* @param initialCapacity the initial capacity of the vector
* @throws IllegalArgumentException if the specified initial capacity
* is negative
capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}

/**
* Constructs an empty vector so that its internal data array
* has size {@code 10} and its standard capacity increment is
* zero.
默认构造函数
*/
public Vector() {
this(10);
}

/**
* Constructs a vector containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this
* vector
* @throws NullPointerException if the specified collection is null
* @since 1.2
创建一个包含collection的Vector
*/
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
+ Vector一共有四个构造函数。 - public Vector(int initialCapacity, int capacityIncrement) :capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。 - public Vector(int initialCapacity) : capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。 - public Vector() : 默认构造函数。 - public Vector(Collection<? extends E> c) : 创建一个包含collection的Vector。

五、LinkedList

  • LinkedList 是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢。另外,他还提供了 List 接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈、队列和双向队列使用。
  • 同样,进去看下源码:
+ ![image-20211104195104098.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/f441d52aaf2eef0adc33c30371c2d76daecc6e37ed4af122203fa877921c6e24)
+ ![image-20211104195131635.png](https://gitee.com/songjianzaina/juejin_p17/raw/master/img/504ad6214bc03a0a8bff1056080ef047b93b85e72d9f51ae736197719c19a595)
+ 首先看下构造函数:


    - 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java复制代码/**
* Constructs an empty list.
*/
public LinkedList() {
}

/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
- 第一个构造方法不接受参数,将header实例的previous和next全部指向header实例。 - 第二个构造方法接收一个Collection参数c,调用第一个构造方法构造一个空的链表,之后通过addAll将c中的元素全部添加到链表中。

路漫漫其修远兮,吾必将上下求索~

如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧~hahah

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%