Set:元素不可以重复,是无序。
Set接口中的方法和Collection一致。
|--HashSet: 内部数据结构是哈希表 ,是不同步的。
如何保证该集合的元素唯一性呢?
是通过对象的hashCode和equals方法来完成对象唯一性的。
如果对象的hashCode值不同,那么不用判断equals方法,就直接存储到哈希表中。
如果对象的hashCode值相同,那么要再次判断对象的equals方法是否为true。
如果为true,视为相同元素,不存。如果为false,那么视为不同元素,就进行存储。
记住:如果元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。
一般情况下,如果定义的类会产生很多对象,比如人,学生,书,通常都需要覆盖equals,hashCode方法。建立对象判断是否相同的依据。
Person共类:
public class Person implements Comparable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(){
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
return name.hashCode()+age*27;
// return 100;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Person))
throw new ClassCastException("類型錯誤");
// System.out.println(this+"....equals..."+obj);
Person p=(Person)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
@Override
public String toString() {
return name+":"+age;
}
@Override
public int compareTo(Object o) {
Person p=(Person)o;
int temp=this.age-p.age;
return temp==0?this.name.compareTo(p.name):temp;
// int temp =this.name.compareTo(p.name);
// return temp==0?this.age-p.age:temp;
/*
if(this.age>p.age)
return 1;
if(this.age<p.age)
return -1;
else{
return this.name.compareTo(p.name);
}
*/
}
}
往hashSet集合中存储Person对象。如果姓名和年龄相同,视为同一个人。视为相同元素。
HashSet集合數據結構是哈希表,所以存储元素的时候,使用的元素的hashCode方法来确定位置,如果位置相同,在通过元素的equals来确定是否相同。
public class HashSetTest {
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add(new Person("lisi4",24));
hs.add(new Person("lisi7",27));
hs.add(new Person("lisi1",21));
hs.add(new Person("lisi9",29));
hs.add(new Person("lisi7",27));
Iterator it = hs.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p);
// System.out.println(p.getName()+"...."+p.getAge());
}
}
}
运行:

HashSet本身是无序的,而可以用LinkedHashSet排序。
public static void main(String[] args) {
HashSet hs=new LinkedHashSet();
hs.add("hahah");
hs.add("hehe");
hs.add("heihei");
hs.add("xixi");
// hs.add("hehe");
Iterator it=hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
运行:

|--TreeSet:可以对Set集合中的元素进行排序。是不同步的。
判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。
TreeSet对元素进行排序的方式一:
让元素自身具备比较功能,就需要实现Comparable接口并覆盖compareTo方法。--Person类实现Comparable接口并覆盖compareTo方法。
TreeSetTest类:
public static void main(String[] args) {
// TreeSet ts=new TreeSet();//以Person对象年龄进行从大到小的排序
ts.add(new Person("zhagnsan",28));
ts.add(new Person("lisi",21));
ts.add(new Person("zhouqi",29));
ts.add(new Person("zhaoliu",25));
ts.add(new Person("wanggu",24));
Iterator it=ts.iterator();
while(it.hasNext()){
Person p=(Person)it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}
运行:

如果不要按照对象中具备的自然顺序进行排序。如果对象中不具备自然顺序。怎么办?
可以使用TreeSet集合第二种排序方式二:
让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。将该类对象作为参数传递给TreeSet集合的构造函数。
按名字排序:
/*
创建了一个根据Person类的name进行排序的比较器
*/
public class ComparatorByName implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
int temp=p1.getName().compareTo(p2.getName());
return temp==0?p1.getAge()-p2.getAge():temp;
// return 1;有序
}
}
TreeSetDemo类:
public static void main(String[] args) {
TreeSet ts=new TreeSet(new ComparatorByName());
ts.add(new Person("zhagnsan",28));
ts.add(new Person("lisi",21));
ts.add(new Person("zhouqi",29));
ts.add(new Person("zhaoliu",25));
ts.add(new Person("wanggu",24));
Iterator it=ts.iterator();
while(it.hasNext()){
Person p=(Person)it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}
运行:

按字符串长度进行排序:
public class ComparatorByLength implements Comparator{
@Override
public int compare(Object o1, Object o2) {
String s1=(String)o1;
String s2=(String)o2;
int temp=s1.length()-s2.length();
return temp==0?s1.compareTo(s2):temp;
}
}
TreeSetDemo:
/*
对字符串进行长度排序
*/
public static void main(String[] args) {
TreeSet ts=new TreeSet(new ComparatorByLength());
ts.add("aaaaa");
ts.add("zz");
ts.add("nbaq");
ts.add("cba");
ts.add("abc");
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
运行:

网友评论