1
|——SortedSet接口——TreeSet实现类
2
Set接口——|——HashSet实现类
3
|——LinkedHashSet实现类
4
HashSet
5
此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。
6
此类为基本操作提供了稳定性能,这些基本操作包括 add、remove、contains 和 size,假定哈希函数将这些元素正确地分布在桶中。对此集合进行迭代所需的时间与 HashSet 实例的大小(元素的数量)和底层 HashMap 实例(桶的数量)的“容量”的和成比例。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。
7
8
我们应该为要存放到散列表的各个对象定义hashCode()和equals();
9
import java.util.HashSet;
10
import java.util.Iterator;
11
public class HashSetTest {
12
public static void main(String[] args)
13
{
14
HashSet hs=new HashSet();
15
/*hs.add("one");
16
hs.add("two");
17
hs.add("three");
18
hs.add("four");*/
19
hs.add(new Student(1,"zhangsan"));
20
hs.add(new Student(2,"lishi"));
21
hs.add(new Student(3,"wangwu"));
22
hs.add(new Student(1,"zhangsan"));
23
24
Iterator it=hs.iterator();
25
while(it.hasNext())
26
{
27
System.out.println(it.next());
28
}
29
}
30
}
31
class Student //HashSet要重写hashCode和equals方法
32
{
33
int num;
34
String name;
35
Student(int num,String name)
36
{
37
this.num=num;
38
this.name=name;
39
}
40
public String toString()
41
{
42
return "num :"+num+" name:"+name;
43
}
44
public int hashCode()
45
{
46
return num*name.hashCode();
47
}
48
public boolean equals(Object o)
49
{
50
Student s=(Student)o;
51
return num==s.num && name.equals(s.name);
52
}
53
}
54
55
TreeSet
56
此类实现 Set 接口,该接口由 TreeMap 实例支持。此类保证排序后的 set 按照升序排列元素,根据使用的构造方法不同,可能会按照元素的自然顺序 进行排序,或按照在创建 set 时所提供的比较器进行排序。
57
是一个有序集合,元素中安升序排序,缺省是按照自然顺序进行排序,意味着TreeSet中元素要实现Comparable接口;
58
我们可以构造TreeSet对象时,传递实现了Comparator接口的比较器对象.
59
import java.util.*;
60
public class TreeSetTest {
61
public static void main(String[] args)
62
{
63
//TreeSet ts=new TreeSet();
64
TreeSet ts=new TreeSet(new Students.compareToStudent());
65
ts.add(new Students(2,"zhangshan"));
66
ts.add(new Students(3,"lishi"));
67
ts.add(new Students(1,"wangwu"));
68
ts.add(new Students(4,"maliu"));
69
70
Iterator it=ts.iterator();
71
while(it.hasNext())
72
{
73
System.out.println(it.next());
74
}
75
}
76
}
77
class Students implements Comparable
78
{
79
int num;
80
String name;
81
Students(int num,String name)
82
{
83
this.num=num;
84
this.name=name;
85
}
86
static class compareToStudent implements Comparator //定义一个内部类来实现比较器
87
{
88
public int compare(Object o1, Object o2) {
89
Students s1=(Students)o1;
90
Students s2=(Students)o2;
91
int rulst= s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 :-1);
92
if(rulst==0)
93
{
94
rulst=s1.name.compareTo(s2.name);
95
}
96
return rulst;
97
}
98
}
99
public int compareTo(Object o) //写具体的比较方法
100
{
101
int result;
102
Students s=(Students)o;
103
result=num >s.num ? 1:(num==s.num ? 0 : -1);
104
if(result==0)
105
{
106
result=name.compareTo(s.name);
107
}
108
return result;
109
}
110
public String toString()
111
{
112
return num+":"+name;
113
}
114
}
115
总结
116
HashSet是基于Hash算法实现的,其性能通常优于TreeSet,我们通常都应该使用HashSet,在我们需要排序的功能时,我门才使用TreeSet
117

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

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步