java集合--Cllection接口中常用方法的使用

Collection接口的简述

Collection 接口是List、Set 和Queue 接口的父接口,该接口里定义的方法既可用于操作Set 集合,也可用于操作List 和Queue 集合。

Collection接口中方法的使用

添加

  1. add(Object obj):将元素objt、添加到集合中;
  2. addAll(addAll(Collection coll)):将一个集合的元素添加到当前集合中。
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author 宇戰天
 * @description
 * 1.add(Object obj)将元素e添加到集合中;
 * 2.addAll(Collection coll)将一个集合的元素添加到当前集合中
 * @create 2020-07-17-12:57
 */
public class AddTest {
    public static void main(String[] args) {
        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));


        System.out.println(coll);
        System.out.println("*******************************************************************");

//       2. addAll(Object coll1)将一个集合的元素添加到当前集合中
        Collection coll1 = new ArrayList();
        coll1.add(new Person("马超",22));
        coll1.add(new Person("张飞",35));
        coll1.add(new Person("刘备",45));

        coll.addAll(coll1);
        System.out.println(coll);


    }
}

运行结果
在这里插入图片描述

获取有效元素的个数

  • int size()
//创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        System.out.println(coll);
        //获取有效元素的个数
        System.out.println(coll.size());

运行结果
在这里插入图片描述

清空集合和判断集合是否为空

  1. void clear();
  2. boolean isEmpty()
        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        System.out.println(coll);
        //获取有效元素的个数
        System.out.println(coll.size());
//        清空当前集合判断当前集合是否为空
        coll.clear();
        System.out.println(coll.isEmpty());

运行结果
在这里插入图片描述

是否包含某个元素

  1. contains(Object obj):判断当前集合中是否包含obj;
  2. containsAll(Collection c)判断当前集合中的所有元素是否存在在当前集合中

contains();

        //创建一个集合
        Collection coll = new ArrayList<>();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));
        boolean b = coll.contains(new Person("赵云",24));
        System.out.println(b);

运行结果
在这里插入图片描述

containsAll();

        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        Collection coll1 = Arrays.asList(new Person("赵云",24),new Person("关羽",34));
        boolean b = coll.containsAll(coll1);
        System.out.println(b);

运行结果
在这里插入图片描述

删除

  1. remove(Object obj)从当前集合中移除obj元素,只会删除找到的第一个元素
  2. removeAll(Collection coll)取当前集合的差集直接修改原集合;

remove(Object obj)

        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));
        System.out.println("移除前");
        System.out.println(coll);
        coll.remove(new Person("赵云",24));
        System.out.println("移除后");
        System.out.println(coll);

运行结果
在这里插入图片描述
removeAll()

        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));
        System.out.println("移除前");
        System.out.println(coll);
        
        Collection coll1 = Arrays.asList(new Person("赵云",24),new Person("关羽",34));
        coll.removeAll(coll1);
        System.out.println("移除后");
        System.out.println(coll);

运行结果
在这里插入图片描述

取两个集合的交集

boolean retainAll(Collection c):把交集的结果存在当前集合中,不影响c

        //创建一个集合
        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        System.out.println("coll集合元素为");
        System.out.println(coll);

        Collection coll1 = Arrays.asList(new Person("马超",22),new Person("张飞",35),new Person("关羽",34));
        System.out.println("coll1中元为");
        System.out.println(coll1);
        System.out.println("两个集合共有的元素为");
        coll.retainAll(coll1);

        System.out.println(coll);

运行结果为
在这里插入图片描述
注意
remove(),removeAll,retainAll()方法会直接修改 当前集合不会影响比较项集合中的元素。

两个集合是否相等

boolean equals(Object obj):比较两个集合中的元素是否完全相等

  Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        Collection coll1 = Arrays.asList(new Person("马超",22),new Person("张飞",35),new Person("关羽",34));

        System.out.println(coll);
        System.out.println(coll1);
        boolean equals = coll.equals(coll1);
        System.out.println(equals);

运行结果
在这里插入图片描述

转成对象数组

Object[] toArray()

        Collection coll = new ArrayList();
//       1. add(Object obj)将元素e添加到coll中
        coll.add(new Person("赵云",24));
        coll.add(new Person("黄忠",56));
        coll.add(new Person("关羽",34));

        Object[] objects = coll.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]+" ");

        }

运行结果
在这里插入图片描述

将数组转换成集合

Arrays.asLIst();

        List<String> objects1 = Arrays.asList(new String[]{"AA","BB","CC"});
        //定义迭代器
        Iterator<String> iterator = objects1.iterator();
        while(iterator.hasNext()){

            System.out.println(iterator.next());
        }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值