Java容器Treeset的两种排序方法
TreeSet支持两种排序方式:自然排序、定制排序1、自然排序:自然排序下,集合元素必须是实现了Comparable接口的类的对象。也就是说,在这种排序方式下需要实现Comparable接口。覆盖此接口中的compareTo(Object obj)方法,定义目标的比较方法package edu.hnsd1.dao;import java.
·
TreeSet支持两种排序方式:自然排序、定制排序
1、自然排序:自然排序下,集合元素必须是实现了Comparable接口的类的对象。
也就是说,在这种排序方式下需要实现Comparable接口。
覆盖此接口中的compareTo(Object obj)方法,定义目标的比较方法
package edu.hnsd1.dao;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 上机实验:CPU有型号、单价、主频等属性
* 构造这样的对象添加到Treeset中,并对其进行降序输出。
* @author Blue Jey
*自然排序方式
*/
public class TreeSetImpl {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet treeset=new TreeSet();
treeset.add(new computer1("联想ThinkPadE431",3333,3));
treeset.add(new computer1("宏基",3000,3));
treeset.add(new computer1("Dell",4000,3));
Iterator itera=treeset.iterator();
while(itera.hasNext()){
System.out.println(itera.next());
}
}
}
class computer1 implements Comparable {
private String Type;//电脑的型号
private int Price;//电脑的单价
private int MainFrequency;//电脑的主频/MHZ
public computer1(String t1,int t2,int t3){
this.setComputer1(t1, t2, t3);
}
public void setComputer1(String t1,int t2,int t3){
this.Type=t1;
this.Price=t2;
this.MainFrequency=t3;
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
int ret=0;
if(!(arg0 instanceof computer1))
throw new RuntimeException("对象不对!");
computer1 temp=(computer1)arg0;
if(this.equals(temp)){
System.out.println("HELLO WORD!");
}
ret=this.Price-temp.Price;
if(ret==0){
ret=this.MainFrequency-temp.MainFrequency;
}
return -ret;
}
public String toString(){
return this.Type+" "+this.Price+" "+this.MainFrequency;
}</span>
2、定制排序:通过构造方法指定TreeSet的比较器进行排序,实现 int compare(T o1,T o2)的比较方法。
package edu.hnsd1.dao;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 上机实验:CPU有型号、单价、主频等属性 构造这样的对象添加到Treeset中,并对其进行降序输出。
*
* @author Blue Jey 自然排序方式
*/
public class TreeSetImpl {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet treeset = new TreeSet(new newComparator());
treeset.add(new computer2("联想ThinkPadE431", 3333, 3));
treeset.add(new computer2("宏基", 3000, 3));
treeset.add(new computer2("Dell", 4000, 3));
Iterator itera = treeset.iterator();
while (itera.hasNext()) {
System.out.println(itera.next());
}
}
}
class computer2 {
private String Type;// 电脑的型号
private int Price;// 电脑的单价
private int MainFrequency;// 电脑的主频/MHZ
public computer2(String t1, int t2, int t3) {
this.setcomputer2(t1, t2, t3);
}
public void setcomputer2(String t1, int t2, int t3) {
this.Type = t1;
this.Price = t2;
this.MainFrequency = t3;
}
public int getPrice() {
return this.Price;
}
public int getMF() {
return this.MainFrequency;
}
public String toString() {
return this.Type + " " + this.Price + " " + this.MainFrequency;
}
}
class newComparator implements Comparator {
@Override
public int compare(Object arg0, Object arg1) {
int ret = 0;
computer2 temp = (computer2) arg0;
computer2 temp1 = (computer2) arg1;
ret = temp.getPrice() - temp1.getPrice();
if (ret == 0) {
ret = temp.getMF() - temp1.getMF();
}
return -ret;
}
}
package com.oracle.javabase.ch4.ex01;
/*数组排序-补充*/
public class TestArraySortAdd {
public static void main(String[] args) {
//补充1:自定义类实现Comparable接口
Person[] persons = new Person[]{
new Person(1, "xiaoli", 21),
new Person(3, "xiaoliu", 25),
new Person(2, "xiaozhao", 23)};
java.util.Arrays.sort(persons);
System.out.println(java.util.Arrays.toString(persons));
//补充2: 自定义类借助Comparator接口
User[] users = new User[]{
new User(1, "xiaoli", 21),
new User(3, "xiaoliu", 25),
new User(2, "xiaozhao", 23)};
java.util.Arrays.sort(users, new UserComparator());
System.out.println(java.util.Arrays.toString(users));
}
}
//自定义类实现Comparable接口
class Person implements Comparable<Person>{//接口,泛型:以后学习
int id;
String name;
int age;
public Person(int id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person p) {
//return this.age < p.age ? -1: (this.age > p.age ? 1 : 0);
if (this.age > p.age){
return 1;
} else if (this.age < p.age){
return -1;
} else {
return 0;
}
}
public String toString(){
return id + "," + name + "," + age;
}
}
class User{
int id;
String name;
int age;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
class UserComparator implements java.util.Comparator<User>{
@Override
public int compare(User o1, User o2) {
return o1.age - o2.age;
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)