分类: Java
2007-11-26 16:08:48
HashSet |
为快速查找而设计的Set。存入HashSet的元素必须定义hashCode()。 |
TreeSet |
保存次序的Set,底层为树结构。使用它可以从Set中提取有序的序列。元素必须实现Comparable接口。 |
LinkedHashSet |
具有HashSet的查询速度,且内部使用链表维护元素的顺序(插入顺序)。在使用迭代器遍历Set时,结果会按插入的次序显示。元素必须定义hashCode()方法。 |
import java.util.HashSet;
import java.util.Iterator;
class HashSetTest {
public static void main(String[] args) {
HashSet
hs.add(new Student(1, "zhangsan"));
hs.add(new Student(2, "lisi"));
hs.add(new Student(3, "wangwu"));
hs.add(new Student(1, "zhangsan"));// 重写hashCode()方法、equals()方法后,hashset不再接受重复的元素
Iterator it =
hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int hashCode() {// 重写hashCode()方法
return num * name.hashCode();
}
public boolean equals(Object o) {// 重写equals()方法
Student s = (Student)
o;
return num == s.num && name.equals(s.name);
}
public String toString() {
return num + ":" + name;
}
} |
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet
ts.add("a");
ts.add("b");
ts.add("c");
Iterator it =
ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
} |
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet
ts.add(new Student(2, "Tom"));
ts.add(new Student(1, "Jeff"));
ts.add(new Student(3, "
ts.add(new Student(3, "Mary"));
ts.add(new Student(1, "Peter"));//compareTo()返回0,则不加入
Iterator it =
ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student implements Comparable {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int compareTo(Object o) {
Student s = (Student)
o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString() {
return num + ":" + name;
}
} |
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
class TreeSetTest {
public static void main(String[] args) {
TreeSet
ts.add(new Student(2, "Tom"));
ts.add(new Student(1, "Jeff"));
ts.add(new Student(3, "
ts.add(new Student(3, "Mary"));
ts.add(new Student(1, "Peter"));
Iterator it =
ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student implements Comparable {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
private class StudentComparator implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 =
(Student) o1;
Student s2 =
(Student) o2;
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
if (result == 0) {
result =
s1.name.compareTo(s2.name);
}
return result;
}
}
public int compareTo(Object o) {
/*Student s = (Student) o;
return
num > s.num ? 1 : (num == s.num ? 0 : -1);*/
StudentComparator
sc=new StudentComparator();
return sc.compare(this, o);
}
public String toString() {
return num + ":" + name;
}
}
|
import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashSetTest2 {
public static void main(String[] args) {
LinkedHashSet
hs.add(new Student(1, "zhangsan"));
hs.add(new Student(2, "lisi"));
hs.add(new Student(3, "wangwu"));
hs.add(new Student(1, "zhangsan"));// 重写hashCode()方法、equals()方法后,hashset不再接受重复的元素
Iterator it =
hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
class Student {
int num;
String name;
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int hashCode() {// 重写hashCode()方法
return num * name.hashCode();
}
public boolean equals(Object o) {// 重写equals()方法
Student s = (Student)
o;
return num == s.num && name.equals(s.name);
}
public String toString() {
return num + ":" + name;
}
} |
import
java.util.Collections;
import
java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetDemo {
static void print(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
SortedSet
Collections.addAll(sortedSet, "one two three four five six seven
eight"
.split(" "));
print(sortedSet);
String
low=sortedSet.first();
String
high=sortedSet.last();
print(low);
print(high);
Iterator
for(int i=0;i<=6;i++){
if(i==3)
low=it.next();
if(i==6)
high=it.next();
else it.next();
}
print(low);
print(high);
print(sortedSet.subSet(low, high));
print(sortedSet.headSet(high));
print(sortedSet.tailSet(low));
}
} |
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/51082