TreeSet和TreeMap
因为Tree是排序的,排序就要用到被排序元素的排序方法(compareTo())而这个方法是comparable接口的抽象方法,因此使用TreeSet加入当中的元素必须实现该接口,然后实现compareTo方法。
TreeSet调用add方法是,会让新添加的元素调用compareTo方法,依次把已有的元素作为参数传入确定大小。
下面我们通过代码来具体理解:(PS:核心主要在对象的comparto方法中)
主类:
package cn.hncu.sort1;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet set=new TreeSet();
Employee e1=new Employee("张三", 20);
Employee e2=new Employee("李四", 21);
Employee e3=new Employee("王五", 18);
Employee e4=new Employee("赵六", 22);
Employee e5=new Employee("李222", 21);
set.add(e1);
set.add(e2);
set.add(e4);
set.add(e3);
set.add(e5);
Iterator it=set.iterator();
while(it.hasNext()){
Employee ee=(Employee) it.next();
System.out.println(ee);
}
}
}
Employee类:
package cn.hncu.sort1;
public class Employee implements Comparable{
private String id;
private String name;
private int age;
static int no=0;
public Employee(String name, int age,String department) {
super();
this.id = id;
this.name = name;
this.age = age;
id=""+(no++);
}
public Employee(String name, int age) {
this(name,age,"none");
}
@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;
Employee other = (Employee) 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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
// @Override
// public int compareTo(Object o) {
// return 1;
// }
@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e=(Employee) o;
if (this.age!=e.age) {
return this.age - e.age;
}else{
return this.id.compareTo(e.id);
}
}else if(o instanceof String){
return 1;
}
return 0;
}
}
演示TreeMap的使用,我们通过第三方,new一个比较器传入TreeMap当中,这样我们的对象中就不要实现接口和写compareto方法了:
主类:
package cn.hncu.sort2;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
import cn.hncu.sort2.Employee;
public class Sort2Demo {
public static void main(String[] args) {
Employee e1=new Employee("张三", 20);
Employee e2=new Employee("李四", 21);
Employee e3=new Employee("王五", 18);
Employee e4=new Employee("赵六", 22);
Employee e5=new Employee("李222", 21);
Comparator comparator=new MyCmp();
TreeMap map =new TreeMap(comparator);
map.put(e1, e1);
map.put(e2, e2);
map.put(e3, e3);
map.put(e4, e4);
map.put(e5, e5);
Set entries =map.entrySet();
Iterator it=entries.iterator();
while(it.hasNext()){
Entry entry=(Entry) it.next();
System.out.println("key="+entry.getKey()+",value="+entry.getValue());
}
}
}
Employee类(没有实现comparable接口和comparto方法的):
package cn.hncu.sort2;
//public class Employee implements Comparable{
public class Employee {
private String id;
private String name;
private int age;
static int no=0;
public Employee(String name, int age,String department) {
super();
this.id = id;
this.name = name;
this.age = age;
id=""+(no++);
}
public Employee(String name, int age) {
this(name,age,"none");
}
@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;
Employee other = (Employee) 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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
// @Override
// public int compareTo(Object o) {
// return 1;
// }
// @Override
// public int compareTo(Object o) {
// if(o instanceof Employee){
// Employee e=(Employee) o;
//
// if (this.age!=e.age) {
// return this.age - e.age;
// }else{
// return this.id.compareTo(e.id);
// }
// }else if(o instanceof String){
// return 1;
// }
// return 0;
//
// }
}
Comparator(比较器)类:
package cn.hncu.sort2;
import java.util.Comparator;
public class MyCmp implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Employee e1=(Employee) o1;
Employee e2=(Employee) o2;
return (e1.getId().compareTo(e2.getId()));
}
}
这里我还要将一个中文排序的问题
中文排序我们要用到一个Collator类。因为collator不好直接new对象,所以我们通过它的工厂方法来new。即:Collator collator=Collator.getInstance();然后拿collator.getcollationkey()的返回值去比。
下面演示:
主类:
package cn.hncu.sort3;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class Sort3Demo {
public static void main(String[] args) {
Comparator comparator=new MyCom2();
TreeMap map=new TreeMap(comparator);
map.put("张三", 10001);
map.put("李四", 11111);
map.put("王五", 12222);
map.put("至死不渝", 1314258);
Set entries=map.entrySet();
Iterator it=entries.iterator();
while(it.hasNext()){
Entry entry=(Entry) it.next();
System.out.println("key="+entry.getKey()+",value="+entry.getValue()); }
}
}
Comparator(比较器)类:
package cn.hncu.sort3;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
public class MyCom2 implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Collator collator=Collator.getInstance();
CollationKey c1=collator.getCollationKey(o1.toString());
CollationKey c2=collator.getCollationKey(o2.toString());
return (c1.compareTo(c2));
}
}