黑马程序员-----集合框架类(二) Set集合和Vector
1.1 Set集合的功能和Collection是一致的(示例1),元素不可以重复,是无序。
示例1:
1 class HashSetDemo
2 {
3 public static void sop(Object obj)
4 {
5 System.out.println(obj);
6 }
7 public static void main(String[] args)
8 {
9
10 HashSet hs = new HashSet();
11
12 sop(hs.add("java01"));
13 sop(hs.add("java01"));
14 hs.add("java02");
15 hs.add("java03");
16 hs.add("java03");
17 hs.add("java04");
18
19 Iterator it = hs.iterator();
20
21 while(it.hasNext())
22 {
23 sop(it.next());
24 }
25 }
26 }
Set:
|--HashSet:数据结构是哈希表。线程是非同步的。
保证元素唯一性的原理:判断元素的hashCode值是否相同。
如果相同,还会继续判断元素的equals方法,是否为true。
|--TreeSet:可以对Set集合中的元素进行排序。
底层数据结构是二叉树。
保证元素唯一性的依据:
compareTo方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性。
元素需要实现Comparable接口,覆盖compareTo方法。
也种方式也成为元素的自然顺序,或者叫做默认顺序。
TreeSet的第二种排序方式。
当元素自身不具备比较性时,或者具备的比较性不是所需要的。
这时就需要让集合自身具备比较性。
在集合初始化时,就有了比较方式。
需求1(示例2,示例3):
往TreeSet集合中存储自定义对象学生。
想按照学生的年龄进行排序。
记住,排序时,当主要条件相同时,一定判断一下次要条件。
示例2:
1 class TreeSetDemo
2 {
3 public static void main(String[] args)
4 {
5 TreeSet ts = new TreeSet();
6
7 ts.add(new Student("lisi02",22));
8 ts.add(new Student("lisi007",20));
9 ts.add(new Student("lisi09",19));
10 ts.add(new Student("lisi08",19));
11 //ts.add(new Student("lisi007",20));
12 //ts.add(new Student("lisi01",40));
13
14 Iterator it = ts.iterator();
15 while(it.hasNext())
16 {
17 Student stu = (Student)it.next();
18 System.out.println(stu.getName()+"..."+stu.getAge());
19 }
20 }
21 }
22
23
24 class Student implements Comparable//该接口强制让学生具备比较性。
25 {
26 private String name;
27 private int age;
28
29 Student(String name,int age)
30 {
31 this.name = name;
32 this.age = age;
33 }
34
35 public int compareTo(Object obj)//实现接口是不是就应该覆盖方法啊?至于拿什么比较我不知道,所以obj
36 {
37
38 //return 0;
39
40 if(!(obj instanceof Student))
41 throw new RuntimeException("不是学生对象");
42 Student s = (Student)obj;
43
44 System.out.println(this.name+"....compareto....."+s.name);
45 if(this.age>s.age)
46 return 1; //返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
47 if(this.age==s.age)
48 {
49 return this.name.compareTo(s.name);//主要条件年龄相同的情况下再比较次要条件名字,字符串的比较
50 }
51 return -1;
52 /**/
53 }
54
55 public String getName()
56 {
57 return name;
58
59 }
60 public int getAge()
61 {
62 return age;
63 }
64 }
示例3:
1 import java.util.*;
2
3 /*
4 当元素自身不具备比较性,或者具备的比较性不是所需要的。
5 这时需要让容器自身具备比较性。
6 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
7
8 当两种排序都存在时,以比较器为主。
9
10 定义一个类,实现Comparator接口,覆盖compare方法。
11
12
13 */
14 class Student implements Comparable//该接口强制让学生具备比较性。
15 {
16 private String name;
17 private int age;
18
19 Student(String name,int age)
20 {
21 this.name = name;
22 this.age = age;
23 }
24
25 public int compareTo(Object obj)
26 {
27
28 //return 0;
29
30 if(!(obj instanceof Student))
31 throw new RuntimeException("不是学生对象");
32 Student s = (Student)obj;
33
34 //System.out.println(this.name+"....compareto....."+s.name);
35 if(this.age>s.age)
36 return 1;
37 if(this.age==s.age)
38 {
39 return this.name.compareTo(s.name);
40 }
41 return -1;
42 /**/
43 }
44
45 public String getName()
46 {
47 return name;
48
49 }
50 public int getAge()
51 {
52 return age;
53 }
54 }
55 class TreeSetDemo2
56 {
57 public static void main(String[] args)
58 {
59 TreeSet ts = new TreeSet();
60
61 ts.add(new Student("lisi02",22));
62 ts.add(new Student("lisi02",21));
63 ts.add(new Student("lisi007",20));
64 ts.add(new Student("lisi09",19));
65 ts.add(new Student("lisi06",18));
66 ts.add(new Student("lisi06",18));
67 ts.add(new Student("lisi007",29));
68 //ts.add(new Student("lisi007",20));
69 //ts.add(new Student("lisi01",40));
70
71 Iterator it = ts.iterator();
72 while(it.hasNext())
73 {
74 Student stu = (Student)it.next();
75 System.out.println(stu.getName()+"..."+stu.getAge());
76 }
77 }
78 }
79
80 class MyCompare implements Comparator
81 {
82 public int compare(Object o1,Object o2)
83 {
84 Student s1 = (Student)o1;
85 Student s2 = (Student)o2;
86
87 int num = s1.getName().compareTo(s2.getName());
88 if(num==0)
89 {
90
91 return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
92 /*
93 if(s1.getAge()>s2.getAge())
94 return 1;
95 if(s1.getAge()==s2.getAge())
96 return 0;
97 return -1;
98 */
99 }
100
101
102 return num;
103
104 }
105 }
2.1 Vector(示例4)
枚举就是Vector特有的取出方式。
发现枚举和迭代器很像。
其实枚举和迭代是一样的。
因为枚举的名称以及方法的名称都过长。
所以被迭代器取代了。
枚举郁郁而终了。
示例4
1 import java.util.*;
2
3 class VectorDemo
4 {
5 public static void main(String[] args)
6 {
7 Vector v = new Vector();
8
9 v.add("java01");
10 v.add("java02");
11 v.add("java03");
12 v.add("java04");
13
14 Enumeration en = v.elements();
15
16 while(en.hasMoreElements())
17 {
18 System.out.println(en.nextElement());
19 }
20 }
21 }