Collection所有方法汇总

本文详细介绍了Java中Collection接口的各种方法,包括添加、获取、删除元素等操作,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Collection所有方法汇总

package com.atguigu.javase.lesson7;

import org.junit.Test;

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

/**
 * 1.添加元素
 * 2.获取元素 & 查找指定元素
 * 3.移除元素
 * 4.工具方法
 */
public class CollectionTest {

    /**
     * 添加一个元素到集合中
     */
    @Test
    public void testAdd(){
        Collection collection = new ArrayList();
        System.out.println(collection.size());

        collection.add("abc");
        collection.add(new Person("Tom",21));
        collection.add(new Person("Jerry",33));
        collection.add(new Person("Mike",23));
        System.out.println(collection.size());
    }

    /**
     * 添加一组元素到集合中
     */
    @Test
    public void testAddAll(){
       Collection collection2 = new ArrayList();
       collection2.add("athebei");

       Collection collection = new ArrayList();
       collection.add("abc");
       collection.add(new Person("Tom",21));
       collection.add(new Person("Jerry",33));
       collection.add(new Person("Mike",23));
       collection2.addAll(collection);
       System.out.println(collection2.size());
    }

    /**
     * 在Collection中无法获取指定的元素,但可以遍历所有的元素
     * 1.使用增强的for循环
     * 2.使用Iterator迭代器
     * 2.1 获取迭代器对象:调用Collection的iterator()方法,获取Iterator接口的对象
     * 2.2 调用Iterator接口的方法进行迭代
     */
    @Test
    public void testIterator(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        collection.add(new Person("Tom",23));
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        for(Object obj : collection){
            System.out.println(obj);
        }

        Iterator iterator = collection.iterator();
        while(iterator.hasNext()){
            Object obj = iterator.next();
            System.out.println(obj);
        }
    }

    /**
     * clear():清空集合
     */
    @Test
    public void testClear(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        collection.add(new Person("Tom",23));
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        collection.clear();
        System.out.println(collection.size());
    }

    /**
     * remove():移除指定元素
     * 通过equals()方法在集合中查找指定的元素,若存在则移除
     */
    @Test
    public void testRemove(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));

        System.out.println(collection.size());

        collection.remove(p);
        System.out.println(collection.size());

        collection.remove(new Person("Jerry",12));//重写equals方法
        System.out.println(collection.size());
    }

    /**
     * removeAll(Collection collection):移除collection中有的元素
     */
    @Test
    public void testRemoveAll(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        System.out.println(collection.size());

        //重写equals方法
        Collection collection2 = new ArrayList();
        collection2.add("ABC");
        collection2.add(new Person("Mike",22));

        collection.removeAll(collection2);
        System.out.println(collection.size());
    }

    /**
     * retainAll(Collection collection):保留collection中有的元素
     */
    @Test
    public void testRetatinAll(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        System.out.println(collection.size());

        //重写equals方法
        Collection collection2 = new ArrayList();
        collection2.add("ABC");
        collection2.add("EDF");
        collection2.add(new Person("Mike",22));

        collection.retainAll(collection2);
        System.out.println(collection.size());
    }

    /**
     * contains(Object o):利用equals()方法比较,查看集合中有没有指定的元素
     */
    @Test
    public void testContains(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        boolean flag = collection.contains(new Person("Jerry",12));
        System.out.println(flag);//true
    }

    /**
     * containsAll(Collecion<?> c):查看集合中有没有指定元素的集合
     */
    @Test
    public void testContainsAll(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));

        Collection collection2 = new ArrayList();
        collection2.add("ABC");
        collection2.add(new Person("Mike",22));
        boolean flag = collection.containsAll(collection2);
        System.out.println(flag);
    }

    /**
     * isEmpty():检验集合是否为空集合
     */
    @Test
    public void testIsEmpty(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));
        System.out.println(collection.isEmpty());
        collection.clear();
        System.out.println(collection.isEmpty());
    }

    /**
     * toArray():把集合元素转为Object对象的数组
     */
    @Test
    public void testToArray(){
        Collection collection = new ArrayList();
        collection.add("ABC");
        Person p = new Person("Tom",23);
        collection.add(p);
        collection.add(new Person("Jerry",12));
        collection.add(new Person("Mike",22));

        Object[] objs = collection.toArray();
        System.out.println(objs.length);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值