说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了):

数组 长度固定 既可以存储基本数据类型,也能存储引用数据类型 一个数组中的元素类型必一致
集合 长度可变 只能存储引用数据类型 一个集合中的元素类型可以是任意的引用类型

一.集合概述

 

 

 

Collection<E>

父接口

 

List<E>

子接口

ArrayList<E>类
Vector<E>类
LinkedList<E>类

Set<E>

子接口

HashSet<E>类
TreeSet<E>类

 

 (List特点:存入顺序和取出顺序一致,存储的元素可以重复)

 (Set特点:元素的顺序无序且唯一,即无序性和唯一性)

 

二.ArrayList类:

(底层是Object数组)

 1.一些基本的方法

 (1)增:add及其重载;

 (2)删:remove(index)移除指定下标的元素,remove(Object)移除指定对象的元素,clear()移除所有;

 (3)改:set(index,Object);

 (4)查:contains();依据equals方法来判断是否包含该元素

 (5)输出:for循环+size()+get(index)方法;

 

特殊注意:

   (1). set返回的该位置上原先的元素。

   (2). remove(Object): 移除此列表中首次出现的指定元素(如果存在)。

 

对以上方法的例子如下:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package day09;
import java.util.ArrayList;
/*
 * ArrayList类基本方法的使用
 * 其他方法请参考API文档
 */
public class Diffrent {
    public static void main(String[] args) {
        ArrayList list=new ArrayList();
        list.add("one");
        list.add(1);
        list.add("two");
        list.add("four");
        list.add(2new String("three"));//在下标为2处添加元素
        list.remove(4);//移除下标为4的元素
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        System.out.println(list.set(1"two"));//set方法的返回值是下标原来的元素 1
        System.out.println(list.contains("two"));//返回类型为Boolean值 true
        System.out.println(list.contains(new String("three")));//这是要说明的一点
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        //indexOf方法返回下标,若不存在返回-1
        System.out.println(list.indexOf("one"));//0
        System.out.println(list.indexOf("two"));//1
        System.out.println(list.indexOf("three"));//2
        System.out.println(list.indexOf(1));//-1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        //重写了toString方法
        System.out.println(list);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        //输出方法
        for(int i=0;i<list.size();i++){
            Object oo=list.get(i);
            System.out.println(oo);
        }   
    }
}

 

 

2.Vector类

(与ArrayList的用法基本一致,这里就不过于多说,只是简单说一下两者的一点区别)如下:

 

 

  ArrayList Vector
推出时间 JDK1.2版本以后 JDK1.0版本就有
线程安全性 非线程安全(不支持多线程) 线程安全
效率 相对较低 效率高
输出方法 for,Iterator for,Iterator

(Vector的相关例子就不在赘述了)

 

3.LinkedList类

(底层是双向链表)

(与ArrayList的用法也基本一致,这里也之强调一下两者之间的区别,LinkedList的相关方法见API文        档即可)

 

ArrayList类 LinkedList类
底层是Object数组 底层是双向链表
经常用于查询操作 经常用于频繁的增加或删除操作

 

 

4.为了更好的学习集合,我们先引入两个小知识点——泛型和迭代器

(1)泛型:为了规范元素类型,避免强制类型转换

(2)迭代器Iterator:用于取出集合中的元素,可以成为打印集合元素的一种方式

   以下是两个知识点的例子:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package day09;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * 这个类说明了泛型和迭代器的作用
 * 程序简单易懂,但很重要
 */
public class Example {
    public static void main(String[] args) {
        List<String> list=new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        //用迭代器+for循环的方式输出集合中的内容
        Iterator<String> it=list.iterator();
        while(it.hasNext()){
            String s=it.next();
            System.out.println(s);
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        //这种方法很常用,底层也是迭代器
        for(String s:list){
            System.out.println(s);
        }
    }
}

 

(3)关于迭代器Iterator应特别注意的地方:

 

a.若在循环里再定义迭代器,会造成死循环,因为hasNext()不移动指针;

 

   b.用Syso(it.next())输出,结果是只有偶数下标才输出,因为next()方法移动指针;

     (当元素总个数为奇数时还会产生NoSuchElementException异常)

 

   c.每一个迭代器只能迭代一次

 

三.HashSet类(底层是Hash表)

  前面在和List中的类做比较时我们已经知道,Set中的类其元素都是无序且唯一的,那么是用什么机制来保证元素的无序性和唯一性呢?答案是:hashCode()和equals()方法来保证

 

1.通过比较HashSet和ArrayList来说明Set的无序性与唯一性

  add()增加元素时 判断元素是否存在或者删除元素时
HashSet类 依据hashCode()和equals() 依据hashCode()和equals()
ArrayList类 都不依据 只依据equals()

 

四.TreeSet类(底层是二叉树)

   TreeSet与HashSet都是实现Se的类,但是TreeSet独特指出是可以完成自动排序,要求存入其中的元素具有可比较性,依据的是compareTo方法。

 

 

比较可以有两种方式:

1. 类 implments Comparable接口,重写compareTo方法。

2. 更灵活的方式:让TreeSet本身具有可比较性:构造中传入一个Comparator的对象,重写compare方法。(这种方式的例子代码如下:)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package day09;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TestComparator {
    public static void main(String[] args) {
        //用于构造方法中的匿名内部类
        Set<Person> set = new TreeSet<Person>(new Comparator<Person>(){
            public int compare(Person o1, Person o2) {
                //根据年龄比较
                return o1.getAge()-o2.getAge();
            }
        });
        Person p1 =  new Person("张非",20);
        Person p2 =  new Person("李斯",30);
        Person p3 =  new Person("张非",20);
        Person p4 =  new Person("王剑",25);
        Person p5 =  new Person("赵敏",23);
        set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);
        set.add(p5);
        Iterator<Person> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}