[英]sorting on map based on values
How to sort a hash map based on values and if the values are same then the sorting should be on the key. 如何根据值对哈希图进行排序, 如果值相同,则排序应在键上。
I tried to use a comparator, but its not giving expected results. 我尝试使用比较器,但未给出预期的结果。
I want the result to be like this 我希望结果像这样
{Bajaj=8.0, Tata=7.99, Maruthi=6.34, Kmart=5.99, Honda=5.78,
Adidas=4.99, Ford=3.99, Nike=3.99, Sears=3.99, Suzuki=3.99,
Apple=2.99, Puma=1.99}
Here's the complete source code: 这是完整的源代码:
import java.util.*;
public class Test {
public static void main(String[] args) {
HashMap<String, Double> map = new HashMap<String, Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);
map.put("Adidas", 4.99);
map.put("Nike", 3.99);
map.put("Puma", 1.99);
map.put("Ford", 3.99);
map.put("Apple", 2.99);
map.put("Sears", 3.99);
map.put("Kmart", 5.99);
map.put("Tata", 7.99);
map.put("Maruthi", 6.34);
map.put("Honda", 5.78);
map.put("Bajaj", 8.0);
map.put("Suzuki", 3.99);
System.out.println("unsorted map: " + map);
sorted_map.putAll(map);
System.out.println("results: " + sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with
// equals.
@Override
public int compare(String a, String b) {
if (base.get(a) > base.get(b)) {
return -1;
} else if (base.get(a) == base.get(b)) {
System.out.println();
if (a.compareTo(b) == -1) {
return -1;
} else if (a.compareTo(b) == 1) {
return 1;
} else {
return 0;
}
} else {
return 1;
} // returning 0 would merge keys
}
}
Here you have two problems in your compare
implementation. 在这里,您的compare
实现中有两个问题。 First, you compare boxed Double
values with ==
: 首先,您将装箱的Double
值与==
进行比较:
else if(base.get(a) == base.get(b))
You should replace this with 您应该将其替换为
else if(base.get(a).equals(base.get(b)))
Second, you check a.compareTo(b)
for specific values like -1
and 1
, but it may return any positive/negative numbers. 其次,您检查a.compareTo(b)
是否为-1
和1
类的特定值,但是它可能返回任何正/负数。 It's better and simpler just to return the result of a.compareTo(b)
instead. 仅返回a.compareTo(b)
的结果会更好,更简单。 Here's the fixed compare
method: 这是固定的compare
方法:
public int compare(String a, String b) {
if (base.get(a) > base.get(b)) {
return -1;
} else if (base.get(a).equals(base.get(b))) {
return a.compareTo(b);
} else {
return 1;
} // returning 0 would merge keys
}
If you want to sort the keys with the same value in case-insensitive manner, just use compareToIgnoreCase
: 如果要以不区分大小写的方式对具有相同值的键进行排序,只需使用compareToIgnoreCase
:
public int compare(String a, String b) {
if (base.get(a) > base.get(b)) {
return -1;
} else if (base.get(a).equals(base.get(b))) {
return a.compareToIgnoreCase(b);
} else {
return 1;
} // returning 0 would merge keys
}
As the sorting order relies on both value and key, use the Map.Entry<String, Double>
entries: 由于排序顺序依赖于值和键,因此请使用Map.Entry<String, Double>
条目:
List<Map.Entry<String, Double>> entries = new ArrayList<>(base.entrySet());
Collections.sort(entries, new Comparator<Map<String, Double>>() {
...
});
Just little bit twisted your compare method of ValueComparator class. 只是有点扭曲了ValueComparator类的比较方法。 This will first sort on values & if values are same then sorting on keys. 这将首先对值进行排序,如果值相同,则对键进行排序。 Hope this helps. 希望这可以帮助。 Output would be something like below: 输出如下所示:
unsorted map: {Adidas=4.99, Bajaj=8.0, Apple=2.99, Ford=3.99, Puma=1.99, Tata=7.99, Nike=3.99, Suzuki=3.99, Honda=5.78, Kmart=5.99, Maruthi=6.34, Sears=3.99} 未分类的地图:{阿迪达斯= 4.99,巴贾杰= 8.0,苹果= 2.99,福特= 3.99,彪马= 1.99,塔塔= 7.99,耐克= 3.99,铃木= 3.99,本田= 5.78,凯马特= 5.99,玛鲁蒂= 6.34,西尔斯= 3.99}
results: {Bajaj=8.0, Tata=7.99, Maruthi=6.34, Kmart=5.99, Honda=5.78, Adidas=4.99, Ford=3.99, Nike=3.99, Sears=3.99, Suzuki=3.99, Apple=2.99, Puma=1.99} 结果:{Bajaj = 8.0,Tata = 7.99,Maruthi = 6.34,Kmart = 5.99,Honda = 5.78,Adidas = 4.99,Ford = 3.99,Nike = 3.99,Sears = 3.99,Suzuki = 3.99,Apple = 2.99,Puma = 1.99 }
import java.util.*;
public class SortValueMap {
public static void main(String[] args) {
HashMap<String, Double> map = new HashMap<String, Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);
map.put("Adidas", 4.99);
map.put("Nike", 3.99);
map.put("Puma", 1.99);
map.put("Ford", 3.99);
map.put("Apple", 2.99);
map.put("Sears", 3.99);
map.put("Kmart", 5.99);
map.put("Tata", 7.99);
map.put("Maruthi", 6.34);
map.put("Honda", 5.78);
map.put("Bajaj", 8.0);
map.put("Suzuki", 3.99);
System.out.println("unsorted map: " + map);
sorted_map.putAll(map);
System.out.println("results: " + sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with
// equals.
@Override
public int compare(String a, String b) {
if(base.get(a).compareTo(base.get(b)) != 0) {
if (base.get(a) > base.get(b)) {
return -1;
} else {
return 1;
}
}
return a.compareTo(b);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.