集合Collection接口中方法的使用(2):

Collection接口:

接口提供给我们的都是抽象方法,那要学这些方法,不妨就借助它的常用实现类ArrayList来学习。因为实现类中这些对应的方法都是从接口中实现过来的嘛,都会遵循这个接口的规范。

在这里插入图片描述

方法的返回类型以及方法的表述、用法等如下:

1、boolean isEmpty()
如果此集合不包含元素,则返回 true 。

2、boolean add(E e)
将元素e添加到集合coll中。

public class BCollection {
    @Test
    public void test() {
        Collection coll = new ArrayList();
        //1、isEmpty() 
       	System.out.println("******************************************************************");
        System.out.println(coll.isEmpty());//如果此集合为空则返回true;

        //2、add(Object e):将元素e添加到集合coll中:
        System.out.println("******************************************************************");
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1, 2, 3});//[I@78e03bb5]
        System.out.println(coll);
	}
}

在这里插入图片描述

3、boolean addAll(Collection<? extends E> c)
将指定集合中的所有元素添加到此集合(可选操作)。

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        Collection coll2 = new ArrayList();
        //addAll(Collection coll):将coll集合中的元素添加到当前的集合中:
        System.out.println("******************************************************************");
        coll.add("coll");
        coll2.add("coll2:");
        coll2.add(123);
        coll2.add("123");
        coll.addAll(coll2);
        System.out.println(coll.size());//2
        //集合中可以出现重复的元素,而且就像“123”和 123 也一样可以出现,null也可以作为元素出现;
        System.out.println(coll);
    }

在这里插入图片描述

4、void clear()
从此集合中删除所有元素(可选操作)。

	//4、clear():清空集合元素
        System.out.println("******************************************************************");
        coll2.clear();
        System.out.println(coll2.size());//0
        System.out.println(coll2);//[]

5、boolean contains(Object o)
如果此集合包含指定的元素,则返回 true 。
关于contains方法使用稍微会复杂一点点,打算单独拿出来写一个博客,朋友可以去看看我的《集合中equals()方法、contains(Object o)方法的使用》博客哦!

6、boolean containsAll(Collection<?> c)
如果此集合包含指定 集合中的所有元素,则返回true。

//6、containsAll(Collection coll):判断形参coll中的所有元素是否都存在于当前集合中
       	@Test
    public void test2(){
        Collection col1 = new ArrayList();
        Collection col2 = new ArrayList();
        Collection col3 = new ArrayList();
        col1.add("aa");//aa
        col1.add(null);//null
        col1.add(123);//自动装箱成Integer 123
        col1.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        col1.add(new int[]{1,2,3});//[I@78e03bb5]

        col2.add(123);
        col2.add("hello");

        col3.add("123");//这里的“123”和col1的123不是同一个类型哦,所以返回为false;
        col3.add("aa");
        System.out.println("返回:"+col1.containsAll(col2));//false
        System.out.println("返回:"+col1.containsAll(col3));//false
    }

7、boolean equals(Object o)
将指定的对象与此集合进行比较以获得相等性。
朋友可以去看看我的《集合中equals()方法、contains(Object o)方法的使用》博客哦!

8、int hashCode()
返回此集合的哈希码值。

@Test
    public void test2(){
        Collection col1 = new ArrayList();
        col1.add("aa");//aa
        col1.add(null);//null
        col1.add(123);//自动装箱成Integer 123
        col1.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        col1.add(new int[]{1,2,3});//[I@78e03bb5]
        int i = col1.hashCode();
        System.out.println(i);//-1870770961
    }

9、Iterator iterator()
返回此集合中的元素的迭代器。

iterator():返回Iterator接口实例,用于遍历集合元素。
主要用来遍历Collection集合的,Map集合不用迭代器。迭代器提供了三种方法:
.hasNext():判断集合是否有下一个元素;
.next():自动指向(获取)下一个元素;
.remove():移除.next()指向的元素;
Collection中才使用迭代器,Map中不适用;

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1,2,3});//[I@78e03bb5]
        //9、iterator():返回Iterator接口的实例,用于遍历集合元素。 主要用来遍历Collection集合的,Map集合不用迭代器。
        //      迭代器提供了三种方法:.hasNext()、.next()、.remove();
        //Collection中才使用迭代器,Map中不适用;
        Iterator iterator = coll.iterator();
//        Object next = iterator.next();//返回的是一个对象
//        Object next1 = iterator.next();
//        System.out.println("next:"+next);//aa
//        System.out.println("next1:"+next1);//null
//        System.out.println("next2:"+iterator.next());//123
        //开发当中迭代器的使用:
        System.out.println(coll);
        while(iterator.hasNext()){
            Object next2 = iterator.next();
            if("aa".equals(next2)){//如果前面迭代到了123,那这里的aa它是匹配不到的。
                iterator.remove();
                break;
            }
        }
        System.out.println(coll);
    }

在这里插入图片描述

10、boolean remove(Object o)
从该集合中删除指定元素的单个实例(如果存在)(可选操作)。

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1,2,3});//[I@78e03bb5]
        //11、remove(Object obj):
        //  11.1如果有多个相同的obj,则移除最先添加的那个obj,不会删除多个,就删一个
        //  11.2它也是通过equals方法来匹配的,匹配到了就删掉它,因为匹配到了就结束了,就不会往下匹配了,
        //      所以只是删掉最前的那个元素;
        System.out.println(coll.size());
        System.out.println(coll);
        coll.remove(123);
        System.out.println(coll);
        System.out.println(coll.size());
    }

在这里插入图片描述
11、boolean removeAll(Collection<?> c)
删除指定集合中包含的所有此集合的元素(可选操作)。

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1,2,3});//[I@78e03bb5]
        //12、removeAll(Collection coll5):从当前集合中移除coll5中所有的元素,删掉了两个集合的交集部分
        System.out.println("******************************************************************");
        System.out.println(coll);
        System.out.println(coll.size());
        Collection coll5 = Arrays.asList("wqq","aa",123,null,"123","qwq");
        System.out.println(coll.removeAll(coll5));
        System.out.println(coll);
        System.out.println(coll.size());
        System.out.println(coll5);
    }

在这里插入图片描述
12、boolean retainAll(Collection<?> c)
获取交集然后把交集的元素放到调用方法的集合中

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        Collection coll2 = new ArrayList();
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1,2,3});//[I@78e03bb5]
        //13、retainAll(Collection coll1):获取交集然后把交集的元素放到调用方法的集合中
        System.out.println("******************************************************************");
        System.out.println(coll);
        coll2.add("123");
        coll2.add(123);
        coll2.add("aa");
        System.out.println(coll2);
        coll.retainAll(coll2);
        System.out.println(coll);
    }

在这里插入图片描述

13、int size()
返回此集合中的元素数。

System.out.println("******************************************************************");
System.out.println(coll.size());//返回int型数据:5

14、Object[] toArray()
返回一个包含此集合中所有元素的数组。

	@Test
    public void test2(){
        Collection coll = new ArrayList();
        coll.add("aa");//aa
        coll.add(null);//null
        coll.add(123);//自动装箱成Integer 123
        coll.add(new Date());//Sat Oct 17 21:02:55 CST 2020
        coll.add(new int[]{1,2,3});//[I@78e03bb5]
        //12、toArray():可用于集合转换成数组,返回的类型是一个Object;
        System.out.println(coll);
        Object[] objects = coll.toArray();
        for(Object obj:objects){
            System.out.println(obj);
        }
    }

在这里插入图片描述

		//拓展:数组转成集合:
        Collection coll6 = Arrays.asList(objects);//数组转成集合
        System.out.println(coll);
        coll.add(objects);
        System.out.println("coll添加了数组objects之后:\n"+coll);//添加的是地址!
        System.out.println("coll6:"+coll6);//数组

在这里插入图片描述以上便是笔者对这几个方法的小总结,不足之处还望各位朋友指出呀!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值