博客
关于我
Collection接口
阅读量:214 次
发布时间:2019-02-28

本文共 8370 字,大约阅读时间需要 27 分钟。

一、单列集合框架结构

|----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 true if this list contains no elements.     *     * @return true 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
list = Arrays.asList(new String[]{ "AA", "BB", "CC"});

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

List
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);        }    }

转载地址:http://cnpi.baihongyu.com/

你可能感兴趣的文章
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>