Java Collection common method of its collection overview

Collection Collections Overview

  • Length of the Java array is fixed, the program can be easily stored and the number of operation a set of data is not fixed, the JDK class library provides a set of Java
  • Unlike arrays that can not store the set of basic data types, and can only store a reference to the object.
  • Array can store the same data types of elements can store a collection of different types of elements

Introduction Collections Framework

Collection set of common functions

java.utiL.Collection Interface

  • All top-level single set of interfaces defined inside a single set of common methods for all of

  • Any method may be used single set of interface Collection

Common methods of the interface Collection

  • public boolean add (E e) the given object to the current collection.
  • public void clear () Clear the set of all the elements.
  • public boolean remove (E e) the given object set in addition to the current album.
  • public boolean contains (E e) determining whether the current set of inclusion given object.
  • public boolean isEmpty () determines whether the current set is empty.
  • public int size () returns the number of elements in a set.
  • public Object [] toArray () the set of elements stored in the array.

add () method

Role: the given object to the current collection.

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);
    }
}
输出结果:
没有进行任何操作的ArrayList集合对象:[]
往集合中添加一个元素后的返回值:true
使用add方法往集合里面添加了元素后:[LeeHua]

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);
    }
}
输出结果:
往集合中添加元素前的集合是:[]
往集合中添加元素后的集合是:[一号, 二号, 三号, 四号]
删除元素"一号"的返回值:true
删除元素"一号"后的集合是:[二号, 三号, 四号]
删除元素"十号"的返回值:false
删除元素"十号"后的集合是:[二号, 三号, 四号]

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);
    }
}
集合:[对象1, 对象2, 对象3, 对象4]
是否包含"对象100":false
是否包含"对象1":true

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);
    }
}
输出结果:
集合是否为空:true
集合是否为空:false

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);
    }
}
输出结果:
collectionSize1 = 0
collectionSize2 = 5

toArray方法

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

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

/**
 * @Author: YiHua Lee
 * @Version: 1.8.0_201       Java SE 8
 * @Application: IntelliJ IDEA
 * @CreateTime: 2020/1/12 14:08
 * @Description:
 */
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);
    }
}
输出结果:
清空集合元素之前:[一号元素, 二号元素, 三号元素, 四号元素, 五号元素]
清空集合元素之后:[]

Guess you like

Origin www.cnblogs.com/liyihua/p/12182562.html