Set接口类简单继承了Collection接口,Set接口下的两个常用子类:HashSet、TreeSet。
范例1:观察HashSet子类特点
import java.util.HashSet;
import java.util.Set;
public class Demo {
public static void main(String[] args) throws Exception {
Set<String> all = new HashSet<>();
all.add("Hello");
all.add("Java");
all.add("Hello");// 重复数据
all.add("World");
// HashSet属于无需排列
System.out.println(all);
}
}
HashSet子类特征属于无序排列
============分割线============
范例2:观察TreeSet子类
import java.util.Set;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
Set<String> all = new TreeSet<>();
all.add("d");
all.add("c");
all.add("e");
all.add("a");
all.add("b");
all.add("b");// 重复数据
System.out.println(all);
}
}
TreeSet没有重复数据,保存的内容自动排序
============分割线============
范例2:TreeSet排序
集合类是对象动态数组,要排序必须使用比较器Comoparable
import java.util.Set;
import java.util.TreeSet;
class Book implements Comparable<Book> {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public String toString() {
return "书名:《" + this.title + "》,价格:" + this.price + "元。";
}
@Override
public int compareTo(Book o) {
if (this.price > o.price) {
return 1;
} else if (this.price < o.price) {
return -1;
} else {
return 0;
}
}
}
public class Demo {
public static void main(String[] args) {
Set<Book> all = new TreeSet<>();
all.add(new Book("Java从入门到精通", 88.6));
all.add(new Book("Java从入门到精通", 88.6));// 完全相同
all.add(new Book("JSP从入门到精通", 88.6));// 价格相同
all.add(new Book("Oracle", 89.6));// 完全不同
all.add(new Book("Android", 66.8));
Object[] obj = all.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
}
}
注:发现TreeSet依靠的是Comparable接口中的compareTo()方法判断是否重复数据,所以价格重复数据不被保存。
============分割线============
范例3:
Comparable接口只能负责TreeSet子类进行重复元素的判断,并不是真正重复元素的验证操作。判断重复元素只能用Object类提供的方法:
· 取得哈希码:public int hasCode();
|—先判断对象的哈希码是否相同,依靠哈希码取得对象内容;
· 对象比较:public boolean equals(Object obj);
|—再将对象的属性进行依次比较
PS:哈希码和对象比较可自动生成:
范例:
import java.util.HashSet;
import java.util.Set;
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
@Override
public String toString() {
return "书名:《" + this.title + "》,价格:" + this.price + "元。";
}
}
public class Demo {
public static void main(String[] args) {
Set<Book> all = new HashSet<>();
all.add(new Book("Java从入门到精通", 88.6));
all.add(new Book("Java从入门到精通", 88.6));// 完全相同
all.add(new Book("JSP从入门到精通", 88.6));// 价格相同
all.add(new Book("Oracle", 89.6));// 完全不同
all.add(new Book("Android", 66.8));
Object[] obj = all.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
}
}