Set集合:
元素存儲無序(不按照存入的順序存儲)。且不能重復
|--**HashSet**: 底層數據結構是hash表。
|--**TreeSet**: ?存入數據按照自然順序排序
Set方法和Collection提供的方法一致。
### HashSet提供的方法:
HashSet是如何保證存儲數據的唯一性呢?
通過元素的兩個方法來完成的,分別是hashCode和equals方法。
1、HashSet在存儲數據時,內部首先調用hashCode方法,如果hashCode一致,在調用equals方法
2、如果hashCode不一致,那么就將數據存儲到hashSet集合中。
~~~
import java.util.*;
public class SetDemo{
public static void main(String args[]){
HashSet hs = new HashSet();
hs.add(new Person("lzl",18));
hs.add(new Person("lzl",18));
hs.add(new Person("hhh",18));
hs.add(new Person("lzl",18));
Iterator it = hs.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
sop(p.getName()+"-------"+p.getAge());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class Person{
private String name ;
private int age;
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
//覆寫hashCode()方法,保證hashCode具有唯一性
public int hashCode(){
System.out.println("hashCode---"+this.name.hashCode()*age);
return this.name.hashCode()*age;
}
//因為要比較的是Person類中的name和age值,所以重寫Object類的equals方法,
public boolean equals(Object obj){
//如果傳入的對象不是Person類,直接返回false
if(!(obj instanceof Person))
return false;
//否則強轉成Person類
Person p = (Person)obj;
//返回name和age的比較值。
return this.name.equals(p.getName()) && this.age == p.getAge();
}
}
~~~
### TreeSet提供的方法
TreeSet集合
1、存儲的數據自身具有比較性。
2、保證數據唯一性的依據是CompareTo()方法return 0。
使用該集合的對象要實現Comparable接口,覆寫CompareTo()方法。
如果CompareTo()return 0表示對象相同。(如例1)
3、如果對象不具有比較器,或者該對象的比較器不是你所想要的。
那么可以通過定義實現比較接口,來實現自定義的比較器(看例2)。
例1:定義一個學生類,存入姓名的年齡,要求按照年齡進行排序。
~~~
import java.util.*;
public class SetDemo{
public static void main(String args[]){
TreeSet ts = new TreeSet();
ts.add(new Student("lzl",18));
ts.add(new Student("lzl",19));
ts.add(new Student("lzl",10));
ts.add(new Student("lzl",20));
ts.add(new Student("lml",20));
ts.add(new Student("lzl",20));
Iterator it = ts.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
sop(s.getName()+"------"+s.getAge());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
/**
因為要將學生類按照年齡來排序,所以要實現Comparable接口,并
覆寫compareTo(Object obj)方法
*/
class Student implements Comparable
{
private String name ;
private int age;
public Student(){
}
public Student(String name,int age){
this.name = name;
this.age = age;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
/**
return:負整數、零或正整數,根據此對象是小于、等于還是大于指定對象。
*/
public int compareTo(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("不是學生類");
Student s = (Student)obj;
if(this.age > s.getAge())
return 1;
if(this.age == s.getAge()){
//年齡如果相同,比較姓名是否相同
return this.name.compareTo(s.getName());
}
return -1;
}
}
~~~
例2:改變學生類的需求,要求讓存儲的學生類按照姓名的字母排序。
~~~
import java.util.*;
public class TreeSetDemo{
public static void main(String args[]){
TreeSet ts = new TreeSet(new myCopara());
ts.add(new Student("lzl",18));
ts.add(new Student("lzl",19));
ts.add(new Student("lzl",10));
ts.add(new Student("lzl",12));
ts.add(new Student("lml",20));
ts.add(new Student("lzl",20));
ts.add(new Student("xy",20));
ts.add(new Student("as",100));
Iterator it = ts.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
sop(s.getName()+"------"+s.getAge());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
/**
實現Comparator接口,并覆寫int compare(T o1, T o2)
*/
class myCopara implements Comparator{
public int compare(Object o1,Object o2){
if(!(o1 instanceof Student) || !(o2 instanceof Student))
throw new RuntimeException("不是學生類型");
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num == 0){
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
}
return num;
}
}
class Student
{
private String name ;
private int age;
public Student(){
}
public Student(String name,int age){
this.name = name;
this.age = age;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
}
~~~
練習:定義一堆字符串,要求按照字符串的長度大小來排序,如果字符串長度相同,那么按照字符串的字母順序排序。
~~~
import java.util.*;
public class TreeSetDemo{
public static void main(String args[]){
TreeSet ts = new TreeSet(new myCopara());
ts.add("abc");
ts.add("abcd");
ts.add("abcde");
ts.add("abce");
ts.add("fffff");
ts.add("fffff");
// ts.add(2);
Iterator it = ts.iterator();
while(it.hasNext()){
String s = (String)it.next();
sop(s);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
/**
實現Comparator接口,并覆寫int compare(T o1, T o2)
*/
class myCopara implements Comparator{
public int compare(Object o1,Object o2){
if(!(o1 instanceof String) || !(o2 instanceof String))
throw new RuntimeException("不是字符串類型");
String s1 = (String)o1;
String s2 = (String)o2;
int size1 = s1.length();
int size2 = s2.length();
int num = new Integer(size1).compareTo(new Integer(size2));
//如果長度相同,那么按照字符串的字母順序排列
if(num == 0){
//System.out.println(s1+"----"+s2);
return s1.compareTo(s2);
}
return num;
}
}
~~~
?