集合和数组的区别:

数组可以存储基本数据类型和对象,但是数组的长度是不变的。集合只能存储对象,长度是可变的。

Java集合主要由两个接口派生而来的:Collection和Map。

Collection接口

  • boolean add(E e)
  • boolean addAll(Collection c)
  • void clear() 清除集合里的所有元素,将集合长度变为0
  • boolean contains(E e)
  • boolean containsAll(Collection c)
  • boolean isEmpty()
  • Iterator iterator() 返回一个Iterator对象用于遍历集合
  • boolean remove(E e)
  • boolean removeAll(Collection)
  • boolean retainAll(Collection c) 从集合中删除集合c里不包含的元素。
  • int size()
  • Object toArray() 将集合转换为数组
 1 import java.util.*;
 2 class  CollectionDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         ArrayList tree= new ArrayList();
 7         tree.add("杨树");
 8         tree.add("桃树");
 9         tree.add("柳树");
10         tree.add("梨树");
11         ArrayList fruiter =new ArrayList();
12         fruiter.add("桃树");
13         fruiter.add("葡萄树");
14         fruiter.add("梨树");
15         fruiter.add("杏树");
16         tree.retainAll(fruiter);
17         System.out.println(tree);
18     }
19 }
20 //运行结果 [桃树, 梨树]

Iterator接口

  • boolean hasNext()
  • Object next() 返回集合里的下一个元素
  • void remove()返回集合里next()上一次返回的元素
 1 import java.util.*;
 2 class  CollectionDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         ArrayList tree= new ArrayList();
 7         tree.add("杨树");
 8         tree.add("桃树");
 9         tree.add("柳树");
10         tree.add("梨树");
11         Iterator it=tree.iterator();
12         while (it.hasNext())
13         {
14             System.out.println(it.next());
15         }
16     }
17 }
18 /*
19 杨树
20 桃树
21 柳树
22 梨树
23 */

 

 

List接口方法摘要

  • void add(int index,E element)
  • boolean addAll(int index,Collection c)
  • E get(int index) 返回index索引的元素
  • int indexOf(Object o)返回元素o第一次出现的索引
  • int lastIndexOf(Object o)
  • Object remove(int index)删除并返回index索引处的元素
  • E set(int index,E element)将索引index的元素替换为element并返回新元素。
  • List subList(int fromIndex,int toIndex)包含头不包含尾
 1 import java.util.*;
 2 class  CollectionDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         ArrayList tree= new ArrayList();
 7         tree.add("杨树");
 8         tree.add("桃树");
 9         tree.add("柳树");
10         tree.add("梨树");
11         System.out.println(tree.subList(0,2));//[杨树, 桃树]
12 
13     }
14 }

ListIterator接口

ListIterator是接口Iterator子接口

方法摘要

  • boolean hasPrevious() 返回该迭代器关联的集合是否还有上一个元素
  • E previous返回列表的前一个元素
  • void add(E e)将指定元素插入列表
  • void remove()
import java.util.*;
class  CollectionDemo
{
    public static void main(String[] args) 
    {
        ArrayList tree= new ArrayList();
        tree.add("杨树");
        tree.add("桃树");
        tree.add("柳树");
        tree.add("梨树");
        ListIterator li=tree.listIterator();
        while (li.hasNext())
        {
            System.out.println(li.next());
        }
        while (li.hasPrevious())
        {
            System.out.println(li.previous());
        }

    }
}

ArrayList类线程不同步

Vector 类线程同步

LinkedList类插入元素速度特别快

 

Set集合类似一个“罐子”元素是无序的。

Set集合没有提供额外的方法与Collection一样。

  • HashSet类

HashSet是不同步的

HashSet判断两个元素相等的标准是:两个对象通过equals方法比较相等,并且两个对象hasCode()方法返回值也相等。

 1 import java.util.*;
 2 class HashSetDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         HashSet hs=new HashSet();
 7         hs.add(new A());
 8         hs.add(new A());
 9         hs.add(new B());
10         hs.add(new B());
11         hs.add(new C());
12         hs.add(new C());
13         System.out.println(hs);
14         //运行结构[C@16e1fb1, A@1, A@1, B@2, C@e2cb55]
15     }
16 }
17 class A
18 {
19     public int hashCode()
20     {
21         return 1;
22     }
23 }
24 class B
25 {
26     public int hashCode()
27     {
28         return 2;
29     }
30     public boolean equals(Object obj)
31     {
32         return true;
33     }
34 }
35 class C
36 {
37     public boolean equals(Object obj)
38     {
39         return true;
40     }
41 }

当hashCode()返回值不同 不调用equals方法只有hashCode()返回值相同时调用equals方法

 1 import java.util.*;
 2 class  HashSetDemo2
 3 {
 4     public static void main(String[] args) 
 5     {
 6         HashSet hs=new HashSet();
 7         hs.add(new Student(15,"张三"));
 8         hs.add(new Student(16,"李四"));
 9         hs.add(new Student(16,"李四"));
10         hs.add(new Student(17,"王五"));
11         Iterator it=hs.iterator();
12         while (it.hasNext())
13         {
14             Student stu=(Student)it.next();
15             System.out.println(stu.getAge()+","+stu.getName());
16         }
17     }
18 }
19 /*
20 调用hasCode()
21 调用hasCode()
22 调用hasCode()
23 调用equals方法
24 调用hasCode()
25 17,王五
26 15,张三
27 16,李四
28 */
29 class Student
30 {
31     private int age;
32     private String name;
33     Student(int age,String name)
34     {
35         this.age=age;
36         this.name=name;
37     }
38     public int getAge()
39     {
40         return age;
41     }
42     public String getName()
43     {
44         return name;
45     }
46     public int hashCode()
47     {
48         System.out.println("调用hasCode()");
49         return name.hashCode()+age*13;
50     }
51     public  boolean equals(Object obj)
52     {
53         System.out.println("调用equals方法");
54         if(!(obj instanceof Student))
55             throw new ClassCastException("类型转换异常");
56         Student s=(Student)obj;
57         return this.name.equals(s.name)&&this.age==s.age;
58     }
59 }
  •  TreeSet类

TreeSet可以使集合处于排序状态

 1 import java.util.*;
 2 class  TreeSetDemo
 3 {
 4     public static void main(String[] args) 
 5     {
 6         TreeSet ts=new TreeSet();
 7         ts.add(new Student(19,"张三"));
 8         ts.add(new Student(17,"李四"));
 9         ts.add(new Student(23,"王五"));
10         ts.add(new Student(19,"赵六"));
11         Iterator it=ts.iterator();
12         while (it.hasNext())
13         {
14             Student stu=(Student)it.next();
15             System.out.println(stu.getAge()+","+stu.getName());
16 
17         }
18     }
19 }
20 class Student implements Comparable 
21 {
22     public int age;
23     public String name;
24     Student(int age,String name)
25     {
26         this.age=age;
27         this.name=name;
28     }
29     public int getAge()
30     {
31         return age;
32     }
33     public String getName()
34     {
35         return name;
36     }
37     public int compareTo(Object obj)
38     {
39         if(!(obj instanceof Student))
40             throw new RuntimeException("不是学生!");
41         Student s=(Student)obj;
42         if(this.age>s.age)
43             return 1;
44         if(this.age==s.age)
45             return this.name.compareTo(s.name);
46         return -1;
47     }
48 }

Comparator接口

 1 import java.util.*;
 2 class  TreeSetDemo2
 3 {
 4     public static void main(String[] args) 
 5     {
 6         TreeSet ts=new TreeSet(new MyComp());
 7         ts.add(new Student(19,"zhangsan"));
 8         ts.add(new Student(17,"lisi"));
 9         ts.add(new Student(23,"wangwu"));
10         ts.add(new Student(19,"zhaoliu"));
11         ts.add(new Student(21,"zhaoliu"));
12         Iterator it=ts.iterator();
13         while (it.hasNext())
14         {
15             Student stu=(Student)it.next();
16             System.out.println(stu.getAge()+","+stu.getName());
17 
18         }
19     }
20 }
21 class Student implements Comparable 
22 {
23     public int age;
24     public String name;
25     Student(int age,String name)
26     {
27         this.age=age;
28         this.name=name;
29     }
30     public int getAge()
31     {
32         return age;
33     }
34     public String getName()
35     {
36         return name;
37     }
38     public int compareTo(Object obj)
39     {
40         if(!(obj instanceof Student))
41             throw new RuntimeException("不是学生!");
42         Student s=(Student)obj;
43         if(this.age>s.age)
44             return 1;
45         if(this.age==s.age)
46             return this.name.compareTo(s.name);
47         return -1;
48     }
49 }
50 class MyComp implements Comparator
51 {
52     public int compare(Object o1,Object o2)
53     {
54         if(!((o1 instanceof Student)&&(o2 instanceof Student)))
55             throw new RuntimeException("不是学生!");
56         Student s1=(Student)o1;
57         Student s2=(Student)o2;
58         int num=s1.getName().compareTo(s2.getName());
59         if(num==0)
60             return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
61         return num;
62     }
63 }

练习:字符串按照长度排序

 1 import java.util.*;
 2 class  TreeSetDemo3
 3 {
 4     public static void main(String[] args) 
 5     {
 6         TreeSet ts=new TreeSet(new MyComp());
 7         ts.add("abc92");
 8         ts.add("dhf");
 9         ts.add("kdsjfkjdks");
10         ts.add("kf");
11         Iterator it=ts.iterator();
12         while (it.hasNext())
13         {
14             System.out.println(it.next());
15 
16         }
17     }
18 }
19 class MyComp implements Comparator
20 { 
21     public int compare(Object o1,Object o2)
22     {
23         String s1=(String)o1;
24         String s2=(String)o2;
25         return new Integer(s1.length()).compareTo(new Integer(s2.length()));
26 
27     }
28 
29 }

 

posted on 2012-06-04 22:34  Lincon Ma  阅读(242)  评论(0编辑  收藏  举报