Java集合体系学习笔记(一)

113 阅读4分钟

Java Collection体系初探

Collection 是什么?

一个/多个元素组成的数据结构,可以进行增删改查

Collection 的构成

Collction由三部分构成

  1. 接口: 表示集合的抽象
  2. 实现: 接口的具体实现
  3. 算法: 对实现接口的对象执行的计算方法

三大重要接口

接口实现
ListArrayList
SetHashSet
MapHashMap

如何查看Collection原码

Collection原码在Java.utils包里

从External Libraries中进入 找到rt.jar/java/util 就是Collection的源代码

image.png

image.png

Collection 接口


The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument.


从上述的JDK内容中我们可以知道

  1. collction是集合体系中的根接口

  2. 集合代表一组对象,称为其元素。

  3. 一些集合是允许有重复元素的,另一些不允许

  4. 一些是有序的 而另一些的无序的

  5. JDK并未提供Collection接口的直接实现,它提供了更细节的子接口(例如List、Set接口),这些接口通常用于传递集合并在需要最大通用性的情况下操作它们。

  6. Collection接口提供了两种构造方法

    • 无参数的构造方法,用于构造一个空的集合
    • 有参数的构造方法,参数也是一个集合,它创建一个新集合,其元素与其参数相同。

从上述介绍中我们可以简单的画一个最基本的Collection类图

image.png

Collection接口的方法

Collection中的方法大致有如下 做一个简单的介绍

  1. add(E element) :将指定的元素添加到集合中。
  2. addAll(Collection<? extends E> collection) :将指定集合中的所有元素添加到当前集合中。
  3. remove(Object element) :从集合中移除指定的元素。
  4. removeAll(Collection<?> collection) :从当前集合中移除与指定集合中相同的所有元素。
  5. clear() :清空集合中的所有元素。
  6. contains(Object element) :判断集合中是否包含指定的元素。
  7. containsAll(Collection<?> collection) :判断当前集合是否包含指定集合中的所有元素。
  8. isEmpty() :判断集合是否为空。
  9. size() :返回集合中元素的数量。
  10. iterator() :返回一个用于遍历集合的迭代器。
  11. toArray() :将集合转换为数组。
  12. retainAll(Collection<?> collection) :保留与指定集合中相同的所有元素,移除其他元素。
  13. equals(Object object) :判断当前集合是否与指定对象相等。
  14. hashCode() :返回当前集合的哈希码值。

image.png

Iterable 和 Iterator接口

Collction中出现了iterator()东西 说这个东西是用于遍历的迭代器 因此简单的研究一下这个东西

从Collection接口的原码中我们可以看到它继承了Iterable接口

image.png

Iterable中又有Iterator构造器

image.png

Iterable接口

功能:实现接口,允许实现对象使用for-loop语句遍历

由于Collection接口继承了Iterable接口 因此所有的Collection实例都

for(int n : nums)

Iterable接口介绍


Implementing this interface allows an object to be the target of the "for-each loop" statement. See For-each Loop


Iterable接口中的方法如下:

  1. Iterator iterator(): 返回一个迭代器,用于遍历
  2. default void forEach(Consumer<? super T> action): 用于实现函数式编程

image.png

Iterator 接口

Iterator接口介绍


An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:

Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

Method names have been improved.


从上述中我们可以知道 Iterator在Collection的更上层 为Collection提供了遍历的功能

在Java中,Iterator是一个接口,它提供了一种遍历集合元素的方式。通过Iterator,我们可以按顺序访问集合中的每个元素,而不需要了解集合的内部结构。下面是一些Iterator接口的常用方法:

  1. boolean hasNext() :判断集合中是否还有下一个元素。
  2. E next() :返回集合中的下一个元素,并将迭代器的位置移动到下一个元素。
  3. void remove() :从集合中移除迭代器最后访问的元素(可选操作 )。

通过对迭代器的了解 我们能够更加全面的画出Collection的类图

image.png

自己实现一个Iterator迭代器

import java.util.Iterator;  
  
public class MyIterator implements Iterator {  
  
    private int[] nums;  
    private int cursor;  

    public MyIterator(int[] nums) {  
        this.nums = nums;  
        this.cursor = 0;  
    }  

    public MyIterator() {  
        this.nums = new int[]{};  
        this.cursor = 0;  
    }  


    @Override  
    public boolean hasNext() {  
        return !(this.cursor == this.nums.length);  
    }  

    @Override  
    public Object next() {  
        return this.nums[cursor++];  
    }  


    public static void main(String[] args) {  
        int[] nums = new int[]{1,2,3,4,5,6,7,8};  
        MyIterator myIterator = new MyIterator(nums);  
        while(myIterator.hasNext()){  
            System.out.println("current element is: " + myIterator.next());  
        }  
    }  
}

image.png