set 與list相比,是無順序的,去除重複項(重寫Hashset和equals方法後)。
package my.lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.List;
public class Student{
private String name;
private int age;
public Student(String name,int age) {
this.name=name;
this.age=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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
Set<Student> stuL=new HashSet<Student>();
Student stu1=new Student("a橘子",1);
Student stu2=new Student("b香蕉",2);
Student stu3=new Student("c蘋果",3);
Student stu4=new Student("d草莓",4);
Student stu5=new Student("e橙子",5);
Student stu6=new Student("f柚子",6);
Student stu7=new Student("g梨",7);
Student stu8=new Student("h葡萄",8);
stuL.add(stu1);
stuL.add(stu2);
stuL.add(stu3);
stuL.add(stu4);
stuL.add(stu5);
stuL.add(stu6);
stuL.add(stu7);
stuL.add(stu8);
Iterator<Student> iter=stuL.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
}
hashSet去除重複是調用對象的HashCode 和equals方法。
package my.lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.List;
public class Student{
private String name;
private int age;
public Student(String name,int age) {
this.name=name;
this.age=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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.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;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args) {
Set<Student> stuL=new HashSet<Student>();
Student stu1=new Student("a橘子",1);
Student stu2=new Student("b香蕉",2);
Student stu3=new Student("c蘋果",3);
Student stu9=new Student("c蘋果",3);
Student stu4=new Student("d草莓",4);
Student stu5=new Student("e橙子",5);
Student stu6=new Student("f柚子",6);
Student stu7=new Student("g梨",7);
Student stu8=new Student("h葡萄",8);
stuL.add(stu1);
stuL.add(stu2);
stuL.add(stu3);
stuL.add(stu4);
stuL.add(stu5);
stuL.add(stu6);
stuL.add(stu7);
stuL.add(stu8);
stuL.add(stu9);
Iterator<Student> iter=stuL.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
}
運行的結果是:
如果不重寫equals方法,hashCode方法,則可以存在相同的對象。