我希望它不是重复的问题,获取排序键和值在地图
我做了两个班,“狗”和“序列”,
“狗”类使用一个比较器进行排序价格优先。 (正常完成)
“序列”类应该在地图上添加排序价格列表(值)+(不重复的国家代码为“钥匙”。这是我正确的给我。
在我的信息,地图应该排序在明确升序钥匙,但是这是不是这样,看到输出
我的目标是在类丝氨酸现在已经整理国家代码与价格的对应值
class Dog implements Comparator<Dog>, Comparable<Dog> {
private int CountryCode;
private double Price;
private String Operator;
Dog() {
}
Dog(int c, double p, String o) {
CountryCode = c;
Price = p;
Operator = o;
}
public int getCountryCode() {
return CountryCode;
}
public double getPrice() {
return Price;
}
public String getOperator() {
return Operator;
}
// Overriding the compareTo method
public int compareTo(Dog d) {
double de = Price - d.getPrice();
if (de > 0.00001)
return 1;
if (de < 0.00001)
return -1;
return 0;
}
// Overriding the compare method to sort by price
public int compare(Dog d, Dog d1) {
double de = d.getPrice() - d1.getPrice();
if (de > 0.00001)
return 1;
if (de < 0.00001)
return -1;
return 0;
}
@Override
public String toString() {
return " " + Price + ":" + Operator + "";
}
}
public class Ser {
/**
* @param args
*/
public static void main(String[] args) {
// Takes a list o Dog objects
ArrayList<Dog> list1 = new ArrayList<Dog>();
Map<Integer, Dog> mp = new HashMap<Integer, Dog>();
list1.add(new Dog(1, 0.9, "A"));
list1.add(new Dog(268, 5.1, "A"));
list1.add(new Dog(46, 0.17, "A"));
list1.add(new Dog(4620, 0.0, "A"));
list1.add(new Dog(468, 0.15, "A"));
list1.add(new Dog(4631, 0.15, "A"));
list1.add(new Dog(4673, 0.9, "A"));
list1.add(new Dog(46732, 1.1, "A"));
list1.add(new Dog(1, 0.92, "B"));
list1.add(new Dog(44, 0.5, "B"));
list1.add(new Dog(46, 0.02, "B"));
list1.add(new Dog(467, 1.0, "B"));
list1.add(new Dog(48, 1.2, "B"));
list1.add(new Dog(1, 0.9, "c"));
list1.add(new Dog(44, 0.4, "c"));
list1.add(new Dog(46, 0.01, "c"));
list1.add(new Dog(467, 0.2, "c"));
// Sorts the array list using comparator
Collections.sort(list1, new Dog());
System.out.println("Sort the Dog class with respect to Price");
for (Dog a : list1)
// printing the sorted list of ages
System.out.println(a.getCountryCode() + " : " + a.getPrice()
+ " : " + a.getOperator());
// Add only those keys from sorted Price but if the key is added, don't
// repeat it
for (int i = 0; i < list1.size(); i++) {
if (!mp.containsKey(list1.get(i).getCountryCode()))
mp.put(list1.get(i).getCountryCode(), list1.get(i));
}
System.out.println("");
System.out.println(" Get Sorted List of Keys and values");
for (Map.Entry<Integer, Dog> e : mp.entrySet()) {
System.out.println(e.getKey() + "" + e.getValue());
}
}
}
输出:排序关于价格的狗类
4620 : 0.0 : A, 46 : 0.01 : c, 46 : 0.02 : B, 4631 : 0.15 : A, 468 : 0.15 : A, 46 : 0.17 : A, 467 : 0.2 : c, 44 : 0.4 : c, 44 : 0.5 : B, 1 : 0.9 : c, 4673 : 0.9 : A, 1 : 0.9 : A, 1 : 0.92 : B, 467 : 1.0 : B, 46732 : 1.1 : A, 48 : 1.2 : B, 268 : 5.1 : A
获取密钥的排序清单和相应的值
4673 0.9:A, 1 0.9:c, 46732 1.1:A, 48 1.2:B, 4631 0.15:A, 4620 0.0:A, 468 0.15:A, 46 0.01:c, 467 0.2:c, 268 5.1:A, 44 0.4:c
第一件事,如果你已经实现了'Comparable',你不需要'Comparator'。其次,将“价格”存储在“双”中是一种犯罪行为。 –
使用名称像CountryCode而不是countryCode的变量也是一种犯罪;) – emka86
这是根据java conventaion –