使用Comparator对象作为参数
需要注意的是:排序方法是针对键的,而不是值的。如果想针对值,需要更麻烦的一些方法(重写一些方法)
java" id="highlighter_101391">
1
2
3
4
5
6
|
TreeMap<Screen,Integer> res = new TreeMap<Screen, Integer>( new Comparator<Screen>() { @Override public int compare(Screen screen1, Screen t1) { // 定义TreeMap的排序方法 return screen1.compareTo(t1); // TreeMap的排序方法是:调用screen的比较方法 } }); |
2. 定义里面的对象的比较方法
继承Comparable接口
1
2
3
4
5
6
7
8
9
10
11
|
public class Screen implements Comparable{ private double size,price; ... @Override public int compareTo(Object s){ // 定义比较方法 Screen screen = (Screen)s; return this .price>screen.getPrice()?- 1 : 1 ; // 返回负整数和正整数 } } |
3. 所有代码
按照Screen的价钱排序
Screen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.util.*; import java.lang.Integer; public class Screen implements Comparable{ private double size,price; public Screen(){} public void setSize( double size){ this .size = size; } public void setPrice( double price){ this .price = price; } public double getSize(){ return this .size;} public double getPrice() { return this .price;} public void show(){ System.out.println( "size is: " + this .size+ "; price is: " + this .price); } @Override public int compareTo(Object s){ // 定义比较方法 Screen screen = (Screen)s; return this .price>screen.getPrice()?- 1 : 1 ; // 返回负整数和正整数 } } |
TestScreen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import java.util.Comparator; import java.util.Scanner; import java.util.TreeMap; import problem2.Screen; public class TestScreen { final static int MAX_NUM = 8 ; public static void main(String []args){ TreeMap<Screen,Integer> res = new TreeMap<Screen, Integer>( new Comparator<Screen>() { @Override public int compare(Screen screen1, Screen t1) { // 定义TreeMap的排序方法 return screen1.compareTo(t1); // TreeMap的排序方法是:调用screen的比较方法 } }); double price, size; Scanner scan = new Scanner(System.in); for ( int i= 0 ;i<MAX_NUM;++i){ Screen screen = new Screen(); size = scan.nextDouble(); price = scan.nextDouble(); screen.setSize(size); screen.setPrice(price); res.put(screen,i); } for (Screen screen:res.keySet()){ screen.show(); } return ; } } // 测试样例(输入) // 1 2 // 3 4 // 5 6 // 7 8 // 9 10 // 1 2 // 3 4 // 3 4 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/dc0e6c3a7c67