java集合之——Collection接口

61 阅读5分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路。

哈喽,大家好!我是Why,一名在读学生,目前刚刚开始进入自己的编程学习生涯。虽然学习起步较晚,但我坚信做了才有0或1的可能。学了一段时间以后也是选择在掘金上分享自己的日常笔记,也希望能够在众多道友的大家庭中打成一片。 本文主要讲解java实现单链表,如果大家读后觉得有用的话,还请大家多多支持博主:欢迎 ❤️点赞👍、收藏⭐、留言💬 ✨✨✨个人主页:JinHuan

前言

老师在教我们学习集合的时候,大多都是以数组为砖,列举了对象数组有那些问题?比如普通的对象数组的最大问题在于数组中的元素个数是固定的,不能动态的扩充大小,所以最早的时候可以通过链表实现一个动态对象数组。但是这样做毕竟太复杂了,所以在 Java 中为了方便用户操作各个数据结构,所以引入了类集的概念,有时候就可以把类集称为 java 对数据结构的实现。
在整个类集中的,这个概念是从 JDK 1.2(Java 2)之后才正式引入的,最早也提供了很多的操作类,但是并没有完整的提出类集的完整概念。

类集中最大的几个操作接口:Collection、Map、Iterator,这三个接口为以后要使用的最重点的接口。所有的类集操作的接口或类都在

java.util 包中。本文主要叙述java最顶级的单值操作父接口——> Collection

此接口定义如下:
public interface Collection extends Iterable

先来看一下Collection的继承树(只展示最常用的)

image-20220408084047832

可以了看出,Collection主要由三大子类型集合接口List,Set以及Queue,本文重点在Collection,所以把针对这三个接口的详细概述链接放到了下面

Java集合之—List

Java集合之—Set

Java集合之—Queue

Collection常用方法

怎么去了解一个接口,那当然是去看它的方法有哪些,Collection接口为我们提供了18个方法,但是只有前面的15个是我们常用的,下面针对前15个常用的方法做出介绍并提供实例

image-20220408091314707

方法简介

No.方法描述
1public boolean add(E e)向集合中插入一个元素
2public boolean addAll(Collection<? extends E> c)向集合中插入一组元素
3public void clear()清空集合中的元素
4public boolean contains(Object o)查找一个元素是否存在
5public boolean containsAll(Collection<?> c)查找一组元素是否存在
6public boolean isEmpty()判断集合是否为空
7public Iterator iterator()为 Iterator 接口实例化
8public boolean remove(Object o)从集合中删除一个对象
9boolean removeAll(Collection<?> c)从集合中删除一组对象
10boolean retainAll(Collection<?> c)这个集合中移除所有不包含在指定集合中的元素的所有元素
11public int size()求出集合中元素的个数
12public Object[] toArray()以对象数组的形式返回集合中的全部内容
13T[] toArray(T[] a)指定操作的泛型类型.并把内容返回
14public boolean equals(Object o)从 Object 类中覆写而来
15public int hashCode()从 Object 类中覆写而来

实例

package com.blog.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @Author jinhuan
 * @Date 2022/4/8 10:40
 * Description:
 * 演示Collection接口中的方法
 */
public class Test01 {
    public static void main(String[] args) {
        Collection collection01 = new ArrayList();

        //  public  boolean add(E e)
        //向Collection01中添加一个元素
        //collection01.add(new String("123"));
        collection01.add("123");
        System.out.println("Collection01-->"+collection01);

        //  public  boolean addAll(Collection<? extends E> c)
        //将Collection02 的元素 添加到 Collection03中
        Collection collection02 = new ArrayList();
        collection02.add("123");
        collection02.add("456");
        collection02.add("789");
        collection02.add("000");

        Collection collection03 = new ArrayList();
        collection03.addAll(collection02);
        System.out.println("Collection03-->"+collection03);

        //public  void clear()
        //清空Collection02中的元素
        System.out.println("Collection02清空前-->"+collection02);
        collection02.clear();
        System.out.println("Collection02清空后-->"+collection02);

        //public  boolean contains(Object o)
        //判断元素 "123" "456" 是否在Collection03中,存在则输出,不存在则提示
        System.out.println(collection03.contains("123") ? "123存在" : "123不存在");
        System.out.println(collection03.contains("456") ? "456存在" : "456不存在");

        //public  boolean containsAll(Collection<?> c)
        //判断Collection03是不是包括Collection01
        System.out.println(collection03.containsAll(collection01) ? "包括" : "不包括");

        //public  boolean isEmpty()
        //判断集合是否为空
        System.out.println(collection01.isEmpty() ? "collection01为空" : "collection01不为空");
        System.out.println(collection02.isEmpty() ? "collection02为空" : "collection02不为空");
        System.out.println(collection03.isEmpty() ? "collection03为空" : "collection03不为空");

        //public  Iterator<E> iterator()
        //实例化Iterator接口
        Iterator iterator = collection03.iterator();
        //迭代
        System.out.println("已获取Collection03迭代器对象,现在开始迭代Collection03:");
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("迭代结束");

        //public  boolean remove(Object o)
        Collection collection04 = new ArrayList();
        collection04.addAll(collection03);
        System.out.println("删除前:"+ collection04);
        collection04.remove("456");
        System.out.println("删除后:"+ collection04);

        //boolean  removeAll(Collection<?> c)
        System.out.println("删除前:"+ collection04);
        //判断Collection01 是否为 Collection04 的子集合,如果是,则总该集合中 删除 子集合所有元素
        if (collection04.containsAll(collection01)){
            collection04.removeAll(collection01);
        }
        System.out.println("删除后:"+ collection04);

        //boolean  retainAll(Collection<?> c)
        Collection collection05 = new ArrayList();
        collection05.addAll(collection03);
        System.out.println("移除前:"+collection05);
        collection05.retainAll(collection01);
        System.out.println("移除后:"+collection05);

        //public  int size()
        System.out.println("Collection01中含有"+collection03.size()+"个元素!");
        System.out.println("Collection03中含有"+collection03.size()+"个元素!");

        //public  Object[] toArray()
        //将Collection06这个集合转换为数组
        Collection collection06 = new ArrayList();
        collection06.addAll(collection03);
        Object[] objects = collection06.toArray();

        //  <T>  T[] toArray(T[] a)
        Collection<Integer> collection07 = new ArrayList<>();
        collection07.add(1);
        collection07.add(2);
        collection07.add(3);
        Integer[] integers = new Integer[]{};
        collection07.toArray(integers);
        //注意,也可以直接使用数组接收
        Integer[] integerss = collection07.toArray(new Integer[]{});

        //public  boolean equals(Object o)
        System.out.println("collection03的ToString方法(调用方法):"+collection03.toString());
        //因为Collection已经重写了Tostring方法,所以直接输出就可以
        System.out.println("collection03的ToString方法(直接输出):"+collection03);

        //public  int hashCode()
        System.out.println("collection03的hashCode方法(哈希值):"+collection03.hashCode());
    }
}

image-20220408112558380

好啦,这就是关于Collection接口最重要的是方法的简单描述了,希望能够帮助到大家,另外,我自己整理了一些自资源(笔记、书籍、软件等)分享在我的公众号上,非常欢迎大家来访白嫖和博主做朋友,一起学习进步!最后别忘啦支持一下博主哦,求三连!❤️❤️❤️

image-20220428105519763