数据结构--Iterable、Collection、List 的常见方法签名以及含义

目录

  • 1. 整体类、数据架构
  • 2. Iterable
  • 3.Collection
  • 4.List

1. 整体类、数据架构

集合(Collection)类:JDK 中实现了常用数据结构及算法的一组类、接口的组合
数据结构--Iterable、Collection、List 的常见方法签名以及含义_第1张图片
集合类对象,主要表现一组用来装载其他对象的容器对象

容器(container)
装在容器中的对象:元素对象(element)、项(item)
遍历(travesal) / 迭代(iterate):某个容器下的所有元素(或者满足某条件的一组元素)
一种专门用来做迭代的对象:迭代器(Iterater)。在线性结构中经常用循环(loop)的方式进行迭代

实现 classes

interface 顺序表 链表 红黑树 哈希表
Set TreeSet HashSet
List ArrayList LinkedList
Queue LInkedList PriorityQueue
Map TreeMap HashMap

2. Iterable

Iterable:具备迭代能力的泛型接口,只要实现了该接口的所有对象,那么就会具备迭代能力,例如,ArrayList 和 LinkedList 就具备迭代的能力
数据结构--Iterable、Collection、List 的常见方法签名以及含义_第2张图片
Iterable 接口是以什么形式体现的呢?
我们来看一下 Iterable 接口的代码
数据结构--Iterable、Collection、List 的常见方法签名以及含义_第3张图片

public interface Iterable<T> {	
//一个对象具备迭代能力怎么体现?
//这个对象提供了一个 iterator() 方法,这个方法返回一个 Iterator 对象:用来迭代的对象(被称为迭代器)     
    Iterator<T> iterator(); 
}

ArrayList是具有迭代能力的,我们简单看一下他是怎么进行迭代的

ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
//要对其进行遍历,暂时不讨论 for 循环遍历,只关心迭代器遍历
Iterator<String> it = list.iterator();
//使用 it 执行的 Iterator 对象,进行迭代

具体实现

public class Demo {
    public static void main(String[] args) {
        Collection<Integer> list = new HashSet<>(Arrays.asList(1, 5, 2, 7, 3, 9));
        
        // x 指向了迭代器对象
        Iterator<Integer> x = list.iterator();
        while (x.hasNext()) {   // x.hasNext()    元素的个数 + 1
            System.out.println(x.next());        // x.next() 元素的个数
        }
    }
}

数据结构--Iterable、Collection、List 的常见方法签名以及含义_第4张图片

public class Demo {
    public static void main(String[] args) {
        Collection<Integer> list = new HashSet<>(Arrays.asList(1, 5, 2, 7, 3, 9));
        
        // x 指向了迭代器对象
        Iterator<Integer> x = list.iterator();
    
        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 1

        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 5

        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 2

        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 7

        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 3

        System.out.println(x.hasNext());    // true
        System.out.println(x.next());       // 9

        System.out.println(x.hasNext());    // false
    }
}

数据结构--Iterable、Collection、List 的常见方法签名以及含义_第5张图片


3.Collection

可以放入同一类东西的容器都被称为 Collection,线性表是 Collection,非线性表同样是 Collection

线性结构独有的特点:
1、元素之间有前后关系
2、容器中的元素,有处于第几位置的说法(以下标形式体现
3、有头部。尾部。中间的概念

Collection 是不一定具备线性结构特点的
数据结构--Iterable、Collection、List 的常见方法签名以及含义_第6张图片
例如一个装满金币的袋子就是一种 Collection,但其中的金币没有第一枚、第二枚…这种说法,所以不是线性结构的

Collection 中的主要方法

方法 含义
int size() 返回容器中的元素个数
boolean isEmpty() 判断容器是不是空的。注意:容器是 empty != 容器是 null
bollean contains(Object o) 容器中是否存在元素 o ,需要遍历容器中所有元素,和 o 进行相等性比较
boolean add(E e) 把元素 e 放入容器中
boolean remove(Object o) 将容器中与 o 相等的一个元素删除掉
boolean addAll(Collection c) 将 c 容器中的所有元素都放到当前容器中
void clear() 清空容器中的所有元素

注意:
1.bollean contains(Object o) :进行相等性比较需要正确的重写 equals 方法
2.boolean add(E e) :由于容器不一定是线性结构,所以可能会放入失败。比如:容器中不允许放入相同的元素,当尝试放入重复的元素时,放入就会失败。返回 true 就放入成功;返回 flase 为放入失败,并且在放入过程中无法明确放到了哪个位置
3.boolean remove(Object o) :再次提到相等性,需要重写 equals 方法,只删除其中一个元素但不保证是哪一个元素


4.List

List 是线性结构,元素之间具有顺序,拥有前后关系,可以进行排序
在这里插入图片描述
List 是继承自 Collection,所以 List 具备迭代和装元素的能力
List 中的部分方法与 Collection 相同,我们列举一些与 Collection 有一些区别的方法

方法 含义
boolean add(E e) 把元素 e 放入 List,用的是尾插操作
void add(int index, E element) 将元素插入指定的 index 位置
boolean remove(Object o) 删除第一个遇到的和 o 相等的元素
E remove(int index) 将[index]位置的元素取出并返回
boolean addAll(Collection c) 将 c 容器中的所有元素按照次序一个一个的尾插到当前 this 容器中
void sort(Comparator< E > c) 以 c 作为元素的比较器,对元素进行原地排序
E get(int index) 返回 index 下标的元素
E set(int index, E element) 用 element 替换 index 位置的元素,并返回 index 位置原来的元素
int inedxOf(E e) 从前往后找,第一个和 e 相等的元素的位置
int lastIndexOf(E e) 从后往前,最后一个和 e 相等的元素所在的位置
List< E > subList(int fromIndex, int toIndex) 将原有线性结构的[fromIndex, toIndex)截取成一个新的线性结构并返回

注意:
1.boolean add(E e):对于线性结构来说,尾插是不可能失败的(内存不够不在讨论范围内),所以返回值永远是 true
2.boolean remove(Object o):判断相等性要正确重写 equals 方法
3.boolean addAll(Collection c) :插入的次序不确定,需要看传入的 Collection 是什么结构


其他数据结构知识✔:
【数据结构】哈希表详解 – 学懂全靠这篇文章啦
【数据结构】二叉树详解
【数据结构】二叉树的遍历(非递归)
【数据结构】五种基本排序算法

你可能感兴趣的:(数据结构,Java知识总结,java,数据结构)