Collection集合概述

时间:2021-09-24
本文章向大家介绍Collection集合概述,主要包括Collection集合概述使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
  1. Java数组的长度是固定的,为了使程序能够方便地存储和操作数目不固定的一组数据,JDK类库提供了Java集合
  2. 与数组不同的是,集合中不能存放基本类型数据,而只能存放对象的引用。
  3. 数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素

集合框架的介绍#

Collection集合的常用功能

java.utiL.Collection接口

1.所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
2.任意的单列集合都可以使用Collection接口中的方法

Collection接口的共性方法
public boolean add(E e)          把给定的对象添加到当前集合中。
public void clear()              清空集合中所有的元素。
public boolean remove(E e)       把给定的对象在当前集合中册除。
public boolean contains(E e)     判断当前集合中是否包合给定的对象。
public boolean isEmpty()         判断当前集合是否为空。
public int size()                返回集合中元素的个数。
public Object[] toArray()        把集合中的元素,存储到数组中。
add()方法

作用:把给定的对象添加到当前集合中。

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

public class DemoCollectionAdd {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();

        // 输出该集合的内容是为空的(其中它重写了toString方法)
        System.out.println("没有进行任何操作的ArrayList集合对象:" + collection);

        // 往ArrayList集合中添加元素,返回值只一个boolean值,一般不用接收这个返回值
        boolean addReturn = collection.add("LeeHua");
        System.out.println("往集合中添加一个元素后的返回值:" + addReturn);
        System.out.println("使用add方法往集合里面添加了元素后:" + collection);
    }
}
remove方法

作用:把给定的对象在当前集合中册除。

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

public class DemoCollectionRemove {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();
        System.out.println("往集合中添加元素前的集合是:" + collection);

        // 往集合中添加元素
        collection.add("一号");
        collection.add("二号");
        collection.add("三号");
        collection.add("四号");
        System.out.println("往集合中添加元素后的集合是:" + collection);

        // 使用remove方法,把给定的对象在当前集合中册除
        // 如果要删除的元素存在该集合,那么就返回true
        // 否则返回false
        boolean removeReturn1 = collection.remove("一号");
        System.out.println("删除元素\"一号\"的返回值:" + removeReturn1);
        System.out.println("删除元素\"一号\"后的集合是:" + collection);

        boolean removeReturn2 = collection.remove("十号");
        System.out.println("删除元素\"十号\"的返回值:" + removeReturn2);
        System.out.println("删除元素\"十号\"后的集合是:" + collection);
    }
}
contains方法

作用:判断当前集合中是否包合给定的对象。

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

public class DemoCollectionContains {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();

        // 往集合中添加元素
        collection.add("对象1");
        collection.add("对象2");
        collection.add("对象3");
        collection.add("对象4");
        System.out.println("集合:" + collection);

        // 使用contains方法,判断当前集合中是否包合给定的对象
        // 如果包合给定的对象,那么就返回true
        // 否则返回false
        boolean containsReturn1 = collection.constains("对象100");
        System.out.println("是否包含\"对象100\":" + containsReturn1);

        boolean containsReturn2 = collection.constains("对象1");
        System.out.println("是否包含\"对象1\":" + containsReturn2);
    }
}
isEmpty方法

作用:判断当前集合是否为空。

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

public class DemoCollectionIsEmpty {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();

        // 判断集合是否为空
        boolean isEmptyReturn1 = collection.isEmpty();
        System.out.println("集合是否为空:" + isEmptyReturn1);

        // 向集合里面添加元素
        collection.add("一号元素");
        // 判断集合是否为空
        boolean isEmptyReturn2 = collection.isEmpty();
        System.out.println("集合是否为空:" + isEmptyReturn2);
    }
}
size方法

作用:返回集合中元素的个数。

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

public class DemoCollectionSize {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();

        // 使用size方法,查看集合中的元素个数
        int collectionSize1 = collection.size();
        System.out.println("collectionSize1 = " + collectionSize1);

        // 往集合中添加元素
        collection.add("一号元素");
        collection.add("二号元素");
        collection.add("三号元素");
        collection.add("四号元素");
        collection.add("五号元素");

        // 使用size方法,再次查看集合中的元素个数
        int collectionSize2 = collection.size();
        System.out.println("collectionSize2 = " + collectionSize2);
    }
}
toArray方法

作用:把集合中的元素,存储到数组中。

public class DemoCollectionToArray {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();

        // 往集合中添加元素
        collection.add("一号元素");
        collection.add("二号元素");
        collection.add("三号元素");
        collection.add("四号元素");
        collection.add("五号元素");

        // 使用toArray方法,把集合中的元素,存储到数组中。
        Object[] collectionToArray = collection.toArray();

        // 遍历输出
        for (int i = 0; i < collectionToArray.length; i++) {
            System.out.println(collectionToArray[i]);
        }
    }
}
clear方法

作用:清空集合中的所用元素

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

public class DemoCollectionClear {
    public static void main(String[] args) {
        // 使用多态,创建一个ArrayList对象
        Collection<String> collection = new ArrayList<>();

        // 往集合中添加元素
        collection.add("一号元素");
        collection.add("二号元素");
        collection.add("三号元素");
        collection.add("四号元素");
        collection.add("五号元素");
        System.out.println("清空集合元素之前:" + collection);

        // 使用clear方法,清空集合中的所用元素
        collection.clear();
        System.out.println("清空集合元素之后:" + collection);
    }
}

转载出处:https://www.cnblogs.com/liyihua/p/12182562.html

原文地址:https://www.cnblogs.com/changanyi/p/15328669.html