//看看实体类吧!
public class Student implements Serializable,Comparable<Student>{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer sid;
private String sname;
private Float score;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((score == null) ? 0 : score.hashCode());
result = prime * result + ((sid == null) ? 0 : sid.hashCode());
result = prime * result + ((sname == null) ? 0 : sname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
//true代表已存在,false代表允许添加(不存在)
//当前对象是否与obj对象相同
if (this == obj)
return true;
//obj对象是否为空
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
//当前对象类型与obj对象类型不相同
Student other = (Student) obj;
if(this.getSid()==other.getSid()) {
return true;
}
return true;
}
public Student(Integer sid, String sname, Float score) {
super();
this.sid = sid;
this.sname = sname;
this.score = score;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", score=" + score + "]";
}
/*@Override
public int compareTo(Student o) {
return -(this.getSid()-o.getSid());
}*/
@Override
public int compareTo(Student o) {
//比较者大于被比较者返回1
//比较者等于被比较者返回0
//比较者小于被比较者返回-1
/*if(this.getSid()>o.getSid())
return 1;
else {
if(this.getSid()==o.getSid())
return 0;
else
return -1;
}*/
//return this.getSid()-o.getSid();
return this.getSname().hashCode()-o.getSname().hashCode();
}
接下来就是他的测试方法(也涵盖了Set的特点的例子代码)
1. Set 1.1 特点:无序、对象不能重复 1.2 遍历 1.2.1 foreach 1.2.2 迭代器 1.3 常用实现类 HashSet
package com.zking.Collection.util;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.zking.Collection.entity.Student;
public class Demo1 {
public static void main(String[] args) {
//Set
//1.特点:无序,唯一
//1)无序:元素添加的顺序与输出的不一致
//2)唯一:不允许添加重复元素(被过滤掉了)
//问题:元素是被覆盖还是过滤
/*Set<String> set=new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.forEach(System.out::println);*/
Set<Student> set=new HashSet<>();
set.add(new Student(1, "a", 100f));
set.add(new Student(2, "b", 70f));
set.add(new Student(3, "c", 80f));
set.add(new Student(1, "a", 90f));
//1)默认使用equals比较,Object类型是引用类型,默认比较的是内存地址
//2)重新hashcode和equals方法,先比较hashcode值是否相同,再比较equals
//2.1)hashcode值相同,则比较equals
//2.2)hashcode值不相同,则不比较equals
/*Student stu=new Student();
stu.equals(null);*/
//set.forEach(System.out::println);
//2.遍历方式(2种)
//1)foreach
for(Student student: set) {
System.out.println(student);
}
System.out.println("-------------");
//2)iterator(迭代器)
//获取迭代器
Iterator<Student> it= set.iterator();
//循环遍历
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
TreeSet:根据某种(规则)对里面的元素进行排序 自然比较接口: java.lang.Comparable String以AscII码进行比较,返回差值
以下就是TreeSet的代码案例(进行升序降序....)
public static void main(String[] args) {
//TreeSet:以某种特定的规则对集合中的某种元素进行排序(升序和降序)
//java.lang.Comparable:自然比较接口
//java.util.Comparator:比较器(推荐)
/*Set<String> set=new TreeSet<>();
set.add("hh");
set.add("mm");
set.add("zz");
set.add("dd");*/
Set<Student> set=new TreeSet<>(new NameComparator());
set.add(new Student(1, "hh", 100f));
set.add(new Student(2, "mm", 70f));
set.add(new Student(3, "zz", 80f));
set.add(new Student(1, "dd", 90f));
set.forEach(System.out::println);
}
package com.zking.Collection.util;
import java.util.Comparator;
import com.zking.Collection.entity.Student;
public class NameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getSid()-o2.getSid();
}
}