对于可变对象的TreeSet排序:
如果我不重写哈希码等于,那么对于所有字段具有相同值的对象,将被视为set中的两个不同对象(默认对象类哈希码方法计算内存位置的哈希值) 。那么为什么TreeSet会有所不同。请参见下面的示例。
如果我只是将整个Set复制到treeSet中,为什么不进行排序。
。
public class Student {
private int id;
private String name;
private int marks;
public Student(int id, String name, int marks) {
super();
this.id = id;
this.name = name;
this.marks = marks;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", marks=" + marks + "]";
}
public static Comparator<Student> idComparator = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return (new Integer(o1.getId())).compareTo(o2.getId());
}
};
public static Comparator<Student> nameComparator = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
};
/*@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + marks;
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 (id != other.id)
return false;
if (marks != other.marks)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}*/
}
。
public class TestMain {
public static void main(String[] args) {
Student s1 = new Student(1, "A", 50);
Student s2 = new Student(1, "A", 50);
Student s3 = new Student(2, "B", 51);
Student s4 = new Student(3, "B", 51);
Student s5 = new Student(4, "B", 41);
Student s6 = new Student(5, "B", 61);
Student s7 = new Student(6, "C", 51);
Student s8 = new Student(7, "D", 46);
Student s9 = new Student(8, "B", 55);
Student s10 = new Student(9, "E", 51);
List<Student> studentList = new ArrayList<>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.add(s5);
studentList.add(s6);
studentList.add(s7);
studentList.add(s8);
studentList.add(s9);
studentList.add(s10);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
System.out.println("List Initial Size: " + studentList.size());
List<Student> studentListAfterFilter = studentList.stream()
.filter(p -> p.getMarks() >= 45 && p.getMarks() <= 55).map(p -> p).collect(Collectors.toList());
System.out.println("After filtering List Size: " + studentListAfterFilter.size());
Set<Student> studentSetAfterFilter = studentList.stream().filter(p -> p.getMarks() >= 45 && p.getMarks() <= 55)
.map(p -> p).collect(Collectors.toSet());
System.out.println("After filtering Set Size: " + studentSetAfterFilter.size());
for(Student student:studentSetAfterFilter){
System.out.println("Set Objects: "+student.toString());
}
Set<Student> studentTreeSetAfterFilter=new TreeSet<>(Student.nameComparator);
studentTreeSetAfterFilter.add(s1);
studentTreeSetAfterFilter.add(s2);
studentTreeSetAfterFilter.add(s3);
studentTreeSetAfterFilter.add(s4);
studentTreeSetAfterFilter.add(s5);
studentTreeSetAfterFilter.add(s6);
studentTreeSetAfterFilter.add(s7);
studentTreeSetAfterFilter.add(s8);
studentTreeSetAfterFilter.add(s9);
studentTreeSetAfterFilter.add(s10);
studentTreeSetAfterFilter.add(s1);
studentTreeSetAfterFilter.add(s2);
studentTreeSetAfterFilter.add(s3);
studentTreeSetAfterFilter.add(s4);
for(Student student:studentTreeSetAfterFilter){
System.out.println("TreeSet Objects: "+student.toString());
}
}
}