Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c1ebc0

Browse files
committed
Map<Key,Value>基于Value值排序
1 parent 3e79e30 commit 7c1ebc0

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ stackoverflow-Java-top-qa
2323
* [如何将String转换为Int](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md)
2424
* [如何分割(split)string字符串](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-split-a-string-in-java.md)
2525
* [在java中如何对比(compare)string](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-do-i-compare-strings-in-java.md)
26+
* [Map<Key,Value>基于Value值排序](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/how-to-sort-a-mapkey-value-on-the-values-in-java.md)
2627

2728
> 编程技巧
2829
@@ -65,7 +66,6 @@ stackoverflow-Java-top-qa
6566
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
6667
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
6768
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
68-
- [How to sort a Map<Key, Value> on the values in Java?](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java)
6969
- [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java)
7070
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
7171
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
##Map<Key,Value>基于Value值排序
2+
3+
###方法1:
4+
使用TreeMap,可以参考下面的代码
5+
```java
6+
public class Testing {
7+
8+
public static void main(String[] args) {
9+
10+
HashMap<String,Double> map = new HashMap<String,Double>();
11+
ValueComparator bvc = new ValueComparator(map);
12+
TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);
13+
14+
map.put("A",99.5);
15+
map.put("B",67.4);
16+
map.put("C",67.4);
17+
map.put("D",67.3);
18+
19+
System.out.println("unsorted map: "+map);
20+
21+
sorted_map.putAll(map);
22+
23+
System.out.println("results: "+sorted_map);
24+
}
25+
}
26+
27+
class ValueComparator implements Comparator<String> {
28+
29+
Map<String, Double> base;
30+
public ValueComparator(Map<String, Double> base) {
31+
this.base = base;
32+
}
33+
34+
// Note: this comparator imposes orderings that are inconsistent with equals.
35+
public int compare(String a, String b) {
36+
if (base.get(a) >= base.get(b)) {
37+
return -1;
38+
} else {
39+
return 1;
40+
} // returning 0 would merge keys
41+
}
42+
}
43+
```
44+
译注:如果不自己写Comparator,treemap默认是用key来排序
45+
46+
###方法2:
47+
48+
先通过linkedlist排好序,再放到LinkedHashMap中
49+
```java
50+
public class MapUtil
51+
{
52+
public static <K, V extends Comparable<? super V>> Map<K, V>
53+
sortByValue( Map<K, V> map )
54+
{
55+
List<Map.Entry<K, V>> list =
56+
new LinkedList<Map.Entry<K, V>>( map.entrySet() );
57+
Collections.sort( list, new Comparator<Map.Entry<K, V>>()
58+
{
59+
public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
60+
{
61+
return (o1.getValue()).compareTo( o2.getValue() );
62+
}
63+
} );
64+
65+
Map<K, V> result = new LinkedHashMap<K, V>();
66+
for (Map.Entry<K, V> entry : list)
67+
{
68+
result.put( entry.getKey(), entry.getValue() );
69+
}
70+
return result;
71+
}
72+
}
73+
```
74+
译注:这两种方法,我简单测试了下,如果map的size在十万级别以上,两者的耗时都是几百毫秒,第二个方法会快一些。否则,第一个方法快一些。因此,如果你处理的map,都是几十万级别以下的大小,两种方式随意使用,看个人喜欢了。
75+
76+
stackoverflow链接:
77+
http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /