Collection接口

一、单列集合框架结构

|----Collection接口:单列集合,用来存储一个一个的对象
           |----List接口:存储有序的、可重复的数据。 -->“动态”数组
                    |----ArrayList、LinkedList、Vector
           |----Set接口:存储无序的、不可重复的数据 -->高中讲的“集合”
                    |----HashSet、LinkedHashSet、TreeSet

在这里插入图片描述

二、Collection接口中常用的方法

添加

add(Object obj)将元素obj添加到集合中

@Test
    public void test1(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new Date());
        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]
    }

addAll(Collection coll)将另一个集合中的所有元素添加到当前集合中

@Test
    public void test1(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new Date());
        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]
        System.out.println(coll.size());//3

        Collection coll2 = new ArrayList();
        coll2.add("sss");
        coll2.add(456);
        coll.addAll(coll2);
        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:30:52 CST 2021, sss, 456]
    }

获取有效元素的个数

size()获取添加元素的个数

@Test
    public void test1(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new Date());
        System.out.println(coll);       //[AA, 123, Thu Mar 18 23:25:47 CST 2021]
        System.out.println(coll.size());//3
    }

是否是空集合

boolean isEmpty()判断是否是空集合

    @Test
    public void test2(){
        Collection coll3 = new ArrayList();
        System.out.println(coll3.isEmpty());        //true
    }
    /**
     * Returns <tt>true</tt> if this list contains no elements.
     *
     * @return <tt>true</tt> if this list contains no elements
     */
    public boolean isEmpty() {
        return size == 0;
    }

清空集合

void clear()清空集合中所有的元素

    @Test
    public void test2(){
        Collection coll3 = new ArrayList();
        coll3.add(111);
        System.out.println(coll3.isEmpty());        //false
        coll3.clear();
        System.out.println(coll3.isEmpty());        //true
    }

是否包含某个元素

boolean contains(Object obj)是通过元素的equals方法来判断是否是同一个对象,向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        System.out.println(coll4.contains(123));        //true
        System.out.println(coll4.contains(new String("hello")));      //true
    }

boolean containsAll(Collection c)也是调用元素的equals方法来比较的。拿两个集合的元素挨个比较。

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Collection coll5 = new ArrayList();
        coll5.add(456);
        coll5.add(false);

        System.out.println(coll4.containsAll(coll5));       //true
   }

删除

boolean remove(Object obj)通过元素的equals方法判断是否是要删除的那个元素。只会删除找到的第一个元素。

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]
        coll4.remove(123);
        System.out.println(coll4);      //[456, hello, false, Person{name='Tom', age=24}]
    }

boolean removeAll(Collection coll)从当前集合中移除集合coll中的所有元素

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Collection coll5 = new ArrayList();
        coll5.add(456);
        coll5.add(false);

        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]
        coll4.removeAll(coll5);
        System.out.println(coll4);      //[123, hello, Person{name='Tom', age=24}]
    }

取两个集合的交集

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

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Collection coll5 = new ArrayList();
        coll5.add(456);
        coll5.add(789);

        System.out.println(coll4);      //[123, 456, hello, false, Person{name='Tom', age=24}]
        coll4.retainAll(coll5);
        System.out.println(coll4);      //[456]
        System.out.println(coll5);      //[456, 789]
   }

集合是否相等

boolean equals(Object obj)比较两个集合是否相等

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Collection coll5 = new ArrayList();
        coll5.add(123);
        coll5.add(456);
        coll5.add(new String("hello"));
        coll5.add(false);
        coll5.add(new Person("Tom",24));

        System.out.println(coll4.equals(coll5));        //true,要注意集合元素是有序的
    }

获取集合对象的哈希值

hashCode()返回当前对象的哈希值

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        System.out.println(coll4.hashCode());       //964169686
    }

转成对象数组

Object[] toArray()返回Object类型数组

    @Test
    public void test3(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Object[] arr = coll4.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

数组也可以转成集合,调用Arrays类的静态方法asList()

 List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});

但是要注意的是下面这种情况

List<int[]> list = Arrays.asList(new int[]{123,456});
System.out.println(list);	//[[I@22927a81]

如果是int类型的数组,输出的结果是一个一维数组的地址,元素为int类型。即相当于把整体结构也就是可变形参new int[]{123,456}当成了一个元素,而不是把数组里面的int类型的值当成多个元素。

应该修改为

List list = Arrays.asList(123,456);
System.out.println(list);		//[123, 456]

//或者使用Integer包装类

List list = Arrays.asList(new Integer[]{123,456});

三、遍历(Iterator迭代器)

  • iterator()返回Iterator接口实例对象,即迭代器对象,用于集合遍历。
  • Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。
  • Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。
  • Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建Iterator 对象,则必须有一个被迭代的集合。
  • 集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
    @Test
    public void IteratorTest(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Iterator iterator = coll4.iterator();

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

迭代器执行原理

		//hasNext()方法判断是否还有下一个元素
        while (iterator.hasNext()){
        	//next()指针下移,然后将下移以后集合位置上的元素返回
            System.out.println(iterator.next());
        }

在这里插入图片描述
Iterator接口remove()方法,可以删除集合的元素,但是是遍历过程中通过迭代器对象remove方 法,不是集合对象的remove方法。

    @Test
    public void IteratorTest(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        Iterator iterator = coll4.iterator();

        while (iterator.hasNext()){
            Object obj = iterator.next();
            if ("Tom".equals(obj)){     //删除集合中Tom数据
                iterator.remove();
            }
        }

        iterator = coll4.iterator();      //需要重新获得一个迭代器从头开始遍历

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

四、foreach循环遍历集合元素

  • 遍历操作不需获取Collection或数组的长度,无需使用索引访问元素。
  • 遍历集合的底层调用Iterator完成操作。
  • foreach还可以用来遍历数组。
	@Test
    public void ForeachTest(){
        Collection coll4 = new ArrayList();
        coll4.add(123);
        coll4.add(456);
        coll4.add(new String("hello"));
        coll4.add(false);
        coll4.add(new Person("Tom",24));

        //for(集合元素的类型 局部变量 : 集合对象)
        for(Object obj : coll4){
            System.out.println(obj);
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值