SortedSet集合:其实现类是TreeSet
SortedMap:实现类是TreeMap(TreeMap的key就是一个TreeSet)
1.无序,不可重复,但存进去的元素可按大小进行排序。
2.SortedSet集合存储元素为什么可以自动排序?
答:因为被存储的元素实现了Comparable接口,sun编写TreeSet集合在添加元素的时候会调用compareTo方法完成比较。
3.SortedSet可以自动排序的另一种方法是:单独编写一个比较器。
附实现排序的代码:
(1).SortedSet实现了Comparable接口
import java.util.*;
class Untitled {
public static void main(String[] args) {
SortedSet ss = new TreeSet();
User user1 = new User(12);
User user2 = new User(14);
User user3 = new User(2);
User user4 = new User(55);
User user5 = new User(76);
User user6 = new User(22);
ss.add(user1);
ss.add(user2);
ss.add(user3);
ss.add(user4);
ss.add(user5);
ss.add(user6);
Iterator it = ss.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
//User 实现接口Comparable
class User implements Comparable{
int age ;
User(int age){
this.age = age;
}
public String toString(){
return "user[age = "+ age +"]";
}
public int compareTo(Object o){
int age1 = this.age;
int age2 = ((User)o).age;
return age1 - age2;
}
}
结果为:
user[age = 2]
user[age = 12]
user[age = 14]
user[age = 22]
user[age = 55]
user[age = 76]
(2).比较器写法
import java.util.*;
class Untitled {
public static void main(String[] args) {
SortedSet ss = new TreeSet(new Comparator(){
//匿名内部类:不推荐使用,因为比较器无法得到重复利用。
public int compare(Object o1 , Object o2){
double price1 = ((Product)o1).price;
double price2 = ((Product)o2).price;
if(price1 > price2){
return 1;
}else if(price1 < price2){
return -1;
}else{
return 0;
}
}
});
Product pro1 = new Product(3.2);
Product pro2 = new Product(22.0);
Product pro3 = new Product(9.8);
Product pro4 = new Product(34.2);
Product pro5 = new Product(1.1);
Product pro6 = new Product(3.4);
ss.add(pro1);
ss.add(pro2);
ss.add(pro3);
ss.add(pro4);
ss.add(pro5);
ss.add(pro6);
Iterator it = ss.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Product{
double price;
Product(double price){
this.price = price;
}
public String toString(){
return "Product[price = "+price+"]";
}
}
// class ProductComparator implements Comparator{
// public int compare(Object o1 , Object o2){
// double price1 = ((Product)o1).price;
// double price2 = ((Product)o2).price;
// if(price1 > price2){
// return 1;
// }else if(price1 < price2){
// return -1;
// }else{
// return 0;
// }
// }
// }
结果:
Product[price = 1.1]
Product[price = 3.2]
Product[price = 3.4]
Product[price = 9.8]
Product[price = 22.0]
Product[price = 34.2]
(3).SortedMap实现了Comparable接口
import java.util.*;
class Untitled {
public static void main(String[] args) {
SortedMap smap = new TreeMap();
Product p1 = new Product("xigua",2.9);
Product p2 = new Product("taozi",9.9);
Product p3 = new Product("pingguo",9.2);
Product p4 = new Product("ningmeng",4.4);
Product p5 = new Product("huanggua",8.0);
smap.put(p1,4);
smap.put(p2,1);
smap.put(p3,2);
smap.put(p4,9);
smap.put(p5,3);
Set keys = smap.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
Object key = it.next();
Object value = smap.get(key);
System.out.println(key + "---->" + value);
}
}
}
class Product implements Comparable{
String name;
double price;
Product(String name , double price){
this.name = name;
this.price = price;
}
public String toString(){
return "Product[name="+name+",price="+price+"]";
}
public int compareTo(Object o){
double price1 = this.price;
double price2 = ((Product)o).price;
if(price1 > price2){
return -1;
}else if(price1 < price2){
return 1;
}else{
return 0;
}
}
}
结果:
Product[name=taozi,price=9.9]---->1
Product[name=pingguo,price=9.2]---->2
Product[name=huanggua,price=8.0]---->3
Product[name=ningmeng,price=4.4]---->9
Product[name=xigua,price=2.9]---->4