Java collections: the use of Collection

1. Overview of java collections

1. Collections and arrays are structures that store and operate multiple data, referred to as java containers.

Note: The storage at this time mainly refers to the storage at the memory level, and does not involve persistent storage (.txt, jpg, avi, database)

2. The characteristics of arrays in storing multiple data:

Once initialized, its length is determined. Once an array is defined, the type of its elements is determined. We can only operate the data of the specified type, for example: String[] arr;int[] arr1;Object[] arr2;

The data stored in the array is ordered and repeatable.

2. Disadvantages of arrays in storing multiple data:

Once initialized, its length cannot be modified. The methods provided in the array are very limited, which is very inconvenient and inefficient for operations such as adding, deleting, and inserting data

The need to obtain the actual number of elements in the array, the array has no ready-made properties or methods available

The characteristics of array storage data: orderly and repeatable. For disorderly and non-repeatable requirements, it cannot be satisfied.

2. Collection framework

1. Common methods of Collection interface

Collection coll= new ArrayList(); 

//add(object e): 将元e添加到集合coLL中
coll.add("AA");
coll.add("BB");
coll.add(123);//自动装箱
coll.add(new Date());

//size(): 获取添加的元囊的个数
System.out.println(coll.size());//4

//addALL(CoLLection coLL1):将coLL1集合中的元囊添加到当前的集合中
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add("cc");
coll.addA11(coll1);
System.out.println(coll.size());//6
System.out.println(coll);

//clear(): 清空集合元素
coll.clear();

//isEmpty():判断当前集合是否为空
System.out.println(coll.isEmpty());

//3.remove(Object obj):从当前集合中移除obj元素。
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person( name: "Jerry", age: 20));
coll.add(new String( original: "Tom"));
coll.add(false);

coll.remove( o: 1234);
System.out.println(coll);
coll.remove(new Person( name: "Jerry", age: 20));
System.out.println(coll);

//4.removeALL(CoLLection coLL1):差集,从当前集合中移除coLL1中所有的元素
Collection coll1 = Arrays.asList(123,456);
coll.removeA11(coll1);
System.out.println(coll);

//5.retainALL(ColLection coLL1):交集: 获取当前集合和coLL1集合的交集,并返回给当前集合
Collection coll1 = Arrays.asList(123,456,789);
coll.retainAll(coll1);
System.out.println(coll); 

//6.equals(Object obj):要想返回true需要当前集合和形参集合的元素都相同
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add(123);
coll1.add(new Person( name: "Jerry", age: 20));
coll1.add(new String( original: "Tom"));
coll1.add(false);
System.out.println(coll.equals(coll1));

//7.hashCode(): 返回当前对象的哈值
System.out.println(coll.hashCode());
//8.集合 --->戴组: toArray()
Object[] arr = coll.toArray();
for(int i = @;i < arr.length;i++){
    System.out.println(arr[il);
}

//拓展:数组 --->集合: 调用Arrays 类的静态方法asList()
List<String> list = Arrays.asList(new String[]("AA","BB","CC"]);
System.out.println(list);

2. Collection element traversal: use Iterator iterator

集合元素的遍历操作,使用送代器Iterator接口:
1.内部的方法: hasNext() 和 next()
2.集合对象每次调用iterator()方法都得到一个全新的送代器对象,默认游标都在集合的第一个元素之前。
3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()

Collection coll = new ArrayList();
coll1.add(456);
coll1.add(123);
coll1.add(new Person( name: "Jerry", age: 20));
coll1.add(new String( original: "Tom"));
coll1.add(false);
//Iterator迭代器遍历集合元素
Iterator iterator = coll.iterator();
//方法
iterator.next():返回下一个元素
iterator.hasNext():是否有下一个元素
//方式-:
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
//报导常: NoSuchELementException(没有下一个元素)
System.out.println(iterator.next());
//方式二:不推荐
for(int i = 0;i < coll.size();i++){
    System.out.println(iterator.next());
}
//方式三:推荐
while(iterator.hasNext()){
  System.out.println(iterator.next());  
}

//错误方式一:报导常: NoSuchELementException、间隔输出
Iterator iterator = coll.iterator();
while((iterator.next()) != null){
    System.out.println(iterator.next());
}}
//错误方式二:死循环,永远是第一个
while (coll.iterator().hasNext()){
    System.out.println(coll.iterator().next());
}

3. List interface framework

List interface: store ordered and repeatable data. --> "Dynamic" array, replace the original array

---ArrayList: As the main implementation class of the List port: thread unsafe, high efficiency. The bottom layer uses object[] elementData to store.

---LinkedList: For frequent insertion and deletion operations, using this type is more efficient than ArrayList. The bottom layer uses a doubly linked list for storage

----Vector: As an ancient implementation class of the List interface: thread-safe, low efficiency. The bottom layer uses object[] elementData to store

Interview questions: Similarities and differences among ArrayList, LinkedList, and Vector?
Same: All three classes implement the List interface, and the characteristics of storing data are the same: Store ordered and repeatable data
differently: see above

1.Common methods of List interface

2. Source code analysis of ArrayList

1. In the case of jdk 7

ArrayList list = new ArrayList(); // 底层创建了长度是10的object[]数组elementData

list.add(123); // elementDatale] = new Integer(123);

list.add(11); // 如果此次的添加导致底层eLementData数组容量不够,则扩容默认情况下,扩容为原来的容量的1.5倍,同时需要将原有数组中的数据复制到新的数组中

结论:建议开发中使用带参的构造器: ArrayList List = new ArrayList(int capacity)

2. jdk 8中

ArrayList的变化:ArrayList list = new ArrayList(); //底层object[] elementData初始化为{},并没有创建长度固定的数组

list.add(123);//第一次调用add()时,底层才创建了长度10的数组,并将数据123添加到elementData

。。。

后续的添加和扩容操作与idk 7 无异。

3. 小结:

jdk7中的ArrayList的对象的创建类似于单例的饿汉式,而jdk8中的ArrayList的对象的创建类似于单例的懒汉式,延迟了数组的创建, 节省内存。

3.LinkedList的源码分析

LinkedList list = new LinkedList(); 内部声明Node类型的first和last属性,默认值为null
list.add(123);//将123封装到Node中,创建了Node对象。

其中,Node定义为:体现了LinkedList的双向链表的说法
private static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;
Node(Node<E> prev,E element,.Node<E> next) {
    this.item = element;
    this.next = next;
    this.prev = prev;
}

4.Vector的源码分析

jdk7和jdk8 中通过Vector()构造器创建对象时,底层都创建了长度为10的数组

在扩容方面,默认扩容为原来的数组长度的2倍。

四、Set接口框架

1.Set接口:存储无序的、不可重复的数据-->高中讲的“集合”

|----HashSet: 作为Set接口的主要实现类: 线程不安全的: 可以存储null值

|----LinkedHashSet: 作为HashSet的子类: 遍历其内部数据时,可以按照添加的顺序遍历,对于频繁的遍历操作,LinkedHashSet微高于HashSet

|----TreeSet: 可以按照添加对象的指定属性,进行排序。

以HashSet为例说明:

1.无序性:不等于随机性。存储的数据在底层数组中并非按照数组索引的顺序添加,而是根据数据的哈希值

2.不可重复性:保证添加的元素按照equals()判断时,不能返回true.即: 相同的元素只能添加一个。

2.添加元素的过程:以Hashset 为例

我们向HashSet中添加元素a,首先调用元素a 所在类的hashCode()方法,计算元素a的哈希值,此哈希值接着通过某种算法计算出在HashSet 底层数组中的存放位置(即为: 索引位置),判断效组此位置上是否已经有元素:

如果此位置上没有其他元素,则元素a添加成功。 --->情况1

如果此位置上有其他元素(或以链表形式存在的多个元素),则比较元素a与元素 的hash值:如果hash值不相同,则元素a添加成功。--->情况2

如果hash值相同,进而需要调用元素a所在类的equlas()方法:equals()返回true,元素a添加失败equals()返回false,则元素a添加成功。--->情况3

对于添加成功的情况2和情况3而言: 元素a与已经存在指定索引位置上数据以链表的方式存储

idk 7 :元素a放到数组中,指向原来的元素。

idk 8 : 原来的元素在数组中,指向元素a

总结:七上八下

说明:
1.Set接口中没有额外定义新的方法,使用的都是Collection 中声明过的方法
2.要求:向set中添加的数据,其所在的类一定要重写hashCode()和equals()
要求:重写的hashCode()equals()尽可能保一致性: 相等的对象必须具有相等的散列码
重写两个方法的小技巧: 对象中用作 equals() 方法比较的 Field,都应该用来计 hashcode

3.LinkerHashSet的使用

LinkedHashSet作为HashSet的子类,在添加数据的同时,每个数据还维护了两个引用,记录此数据前一个数据和后一个数据。

4.TreeSet的使用

a. 向TreeSet 中添加的数据,要求是相同类的对象,可以确保集合元素处于排序状态

b. 两种排序方式:自然排序和定制排序

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/128907583