java中的集合之Collection(一)

来源:互联网 发布:淘宝怎么进卖家中心 编辑:程序博客网 时间:2024/06/07 06:14

集合

一、常见数据结构

1、线性表

• 顺序线性表数组(一块连续的内存空间):查询快,增删慢

• 链式线性表:查询慢,增删快

2、单向链表

3、双向链表

4、 栈:LIFO=last in first out  (先进后出或者后进先出)

5、 队列:FIFO=first in first out(先进先出)

 

二、什么是集合?

 就是一组存放对象的容器。当需要对多个对象统一管理时,就需要集合对象。

三、数组与集合

1、数组:

• 查询块,增删慢。

• 相同数据类型,且定长。

• 数组元素既可以为基本数据类型,也可以为引用类型。

2、集合

• 集合可以为不同类型(一般为相同类型,或自动转换),且长度自增。

• 集合中的元素只能为引用类型或者说Object及其子类。集合中不能有基本数据类型的元素。(基本类型从jdk1.5以后会自动提升为包装类型存储在集合中)

四、集合层次结构

  下面列出经常用的一些子接口及子类

1、Collection

• List:有序的,可重复的,允许有多个null

– ArrayList / Vector

– LinkedList

• Set:无序,不重复的,只允许有一个null

– HashSet

– TreeSet

2、Map

• TreeMap

• HashMap / Hashtable

• Properties


本章主要讲解Collection接口和它的子接口List以及List接口的实现类ArrayList、Vcetor和Linkedist。

首先看Collection接口,文档中介绍的:Collection 层次结构 中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如SetList)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。 

下面看看Collection里面包含的方法:

它提供了一些让我们对集合进行增、删、查、改的方法,还提供一些判断集合自身的属性的方法及与与数组之间的转换。
方法摘要 booleanadd(E e)
          确保此 collection 包含指定的元素(可选操作)。 booleanaddAll(Collection<? extendsE> c)
          将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 voidclear()
          移除此 collection 中的所有元素(可选操作)。 booleancontains(Object o)
          如果此 collection 包含指定的元素,则返回 true booleancontainsAll(Collection<?> c)
          如果此 collection 包含指定 collection 中的所有元素,则返回 true booleanequals(Object o)
          比较此 collection 与指定对象是否相等。 inthashCode()
          返回此 collection 的哈希码值。 booleanisEmpty()
          如果此 collection 不包含元素,则返回 true Iterator<E>iterator()
          返回在此 collection 的元素上进行迭代的迭代器。 booleanremove(Object o)
          从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 booleanremoveAll(Collection<?> c)
          移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 booleanretainAll(Collection<?> c)
          仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 intsize()
          返回此 collection 中的元素数。 Object[]toArray()
          返回包含此 collection 中所有元素的数组。<T> T[]toArray(T[] a)
          返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同通过代码来演示一下

package com.jcxy.demo11;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** *  * 测试Collection中的方法 *   * @author  Mr Wang * @version  [V1.00, 2015-12-11] * @see  [相关类/方法] * @since V1.00 */public class Test{    @SuppressWarnings("unchecked")    public static void main(String[] args)    {        //首先要new一个接口的具体的实现类对象        Collection col1 = new ArrayList();                //接下来才可以测试子类实现的抽象接口中的方法        //1-添加一些对象到集合中        col1.add("damon");        col1.add("rose");        col1.add(new Object());        col1.add(new Integer(1));        col1.add(2);//基本类型从jdk1.5以后会自动提升为包装类型存储在集合中        col1.add(false);        col1.add(new Student(10));        col1.add(new Student(11));        col1.add(new Student(12));        col1.add(new Student(11));                Collection col2 = new ArrayList();        col2.add("jack");        col2.add("linda");                //2-将传入的集合中的全部元素添加到集合中        col1.addAll(col2);        //3-判断元素中是否包含指定元素        System.out.println(col1.contains("rose"));//true                //因为容器中只能存放引用类型的对象,所以,在判断是否包含某个对象值的时候,需要该对象所属的类        //必须要重写equals方法。        System.out.println(col1.contains(new Student(11)));//true        System.out.println(col1.contains(new Student(13)));//false                //4-判断集合中是否包含另外一个集合中的全部元素        System.out.println(col1.containsAll(col2));//true                //5-如果此 collection 不包含元素,则返回 true        System.out.println(col1.isEmpty());//false                //6-返回在此 collection 的元素上进行迭代的迭代器        //通俗的讲就是遍历集合中元素,Collection集合实现Iterator接口        Iterator it =  col1.iterator();        //判断是否有下一个对象        while(it.hasNext())        {            //返回下一个对象            Object o = it.next();//            String str = (String)it.next();如果想得到其他类型,必须要强转            System.out.println("iterator demo:"+o.toString());        }                //7-移除集合中的单个对象,返回boolean值        System.out.println(col1.remove("jack"));//移除成功返回true                        //8-移除此 col1 中那些也包含在指定 col2 中的所有元素        System.out.println(col1.removeAll(col2));//将集合col1中的元素也存在于集合col2里的元素移除,成功返回true                //9-仅保留此 col1 中那些也包含在指定 col2 的元素        System.out.println(col1.retainAll(col2));//                //10- 返回此 collection 中的元素数        System.out.println(col1.size());//0        System.out.println(col2.size());//2                //11-返回包含此 col2 中所有元素的数组        Object[] obj = col2.toArray();                for(Object o : obj)            System.out.println(o);                System.out.println( obj.length);            }}

今天的Collection方法介绍完了,下一节将会介绍Collection子接口list的使用。


1 0