Set集合及其实现类
一、Set集合
1.Set集合按照无序、不允许重复的方式存放对象。Set集合是无序的,没有下标,所以没有**get()**方法。
2.存储特点:无序存储,且不可以存储相同的元素(排重),不能通过下标访问。
二、Set集合的常见实现类及其应用
Set< T >集合实现类:基于散列结构(哈希表)的HashSet类和基于查找树结构的TreeSet类。
1.HashSet的底层结构
HashSet基于一种著名的、可以实现快速查找的散列表( Hash table )结构。
散列表也称哈希表,它采用按照对象的取值计算对象存储地址的策略,实现对象的“定位”存放,相应也提高了查找效率。
2.HashSet 应用
建立Student类:
public class Student {
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 Student() {}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Student [ name = " + name + " , age = " +0 age + " ]";
}
}
import java.util.HashSet;
import java.util.Set;
public class Demol {
public static void main(String[] args) {
Set< Student > set = new HashSet< Student >();
set.add(new Student("凤一",18));
set.add(new Student("凤二",18));
set.add(new Student("凤三",18));
set.add(new Student("凤一",18));
for(Student student : set) {
System.out.println(student);
}
}
}
运行结果:
Student [name = 凤三,age = 18]
Student [name = 凤一,age = 18]
Student [name = 凤二,age = 18]
Student [name = 凤一,age = 18]
从上述代码的运行结果可以看出,重复字符被存进了HashSet集合中,为什么会这样呢?原因是String类本身已经重写了hashCold()和equals()方法。我们需要在Student类中重写hashCode()方法用于获取元素的存储位置,重写equals()方法用于在两个元素位置相同的时候比较两个元素是否相等。
在Student类中重写hashCode()和equals()方法:
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name = = null) ? 0: name.hashCode());
return result;
}
public boolean equals(Object obj) {
if(this = = obj){
return true;
}
if(obj = = null) {
return false;
}
if(getClass() ! = obj.getClass()) {
return false;
}
Student other = (Student) obj;
if(name = = null) {
if(other.name ! = null) {
return false;
}
else if( ! name.equals(other.name)) {
return false;
}
}
return true;
}
这样就避免了重复,运行结果为:
Student [name = 凤三,age = 18]
Student [name = 凤二,age = 18]
Student [name = 凤一,age = 18]
TreeSet 及其应用
TreeSet是基于查找树结构(回忆二叉树),具有排序功能的Set集合。
public class Student {
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 Student() {}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Student [name = "+ name +" , age = " + age +"]";
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name = = null) ? 0:name.hashCode());
return result;
}
public boolean equals(Object obj) {
if(this = =obj) {
return ture;
}
if(obj = = null) {
return false;
}
if(getClass() ! = obj.getClass()) {
return false;
}
Student other = (Student) obj;
if(name = = null) {
if(other.name ! = null) {
return false;
}
else if( ! name.equals(other.name)) {
return false;
}
return true;
}
public int compareTo(Student o) {
if(this.age = = o.age) {
return this.name.compareTo(o.name);
}
return this.age-o.age;
}
}
import java.util.Set;
import java.util.TreeSet;
public class Demo2 {
public static void main(String[] args) {
Set< Student > set = new HashSet< Student >();
set.add(new Student("凤一",18));
set.add(new Student("凤二",18));
set.add(new Student("凤三",18));
set.add(new Student("凤四",18));
for(Student student : set) {
System.out.println(student);
}
}
}