MAPは、辞書または連想配列などとも呼ばれるキー価値ペアのコレクションであり、最も一般的なデータ構造の1つです。 Javaの価値でマップを並べ替える方法は?簡単に思えますが、簡単ではありません!
たとえば、マップでは、キーは単語を表す文字列タイプであり、値はintタイプであり、単語が表示される回数を表します。これで、単語が表示される回数で並べ替えたいと思います。
Map Map = new Treemap(); Map.put( "me"、1000); map.put( "and"、4000); map.put( "you"、3000); map.put( "Food"、10000); Map.put( "Hungry"、5000); Map.put( "Later"、6000);
価値による並べ替えの結果は次のとおりです。
キーValueMe 1000You 3000および4000Hungry 5000Later 6000Food 10000
まず第一に、sortedmapは値でソートされたマップではなく、キーでソートされたマップであるため、sortedmap構造を採用することはできません。私たちが望むのは、値でソートされたマップです。
sortedmapでこれを行うことはできませんでしたか?
いいえ、マップはキーによってソートされているためです。
方法1:
次のJavaコード:
Import java.util.iterator; import java.util.set; import java.util.treeset; public class main {public static void main(string [] args){set set = new Treeset(); set.add(new Pair( "me"、 "1000")); set.add(new Pair( "and"、 "4000")); set.add(new Pair( "you"、 "3000")); set.add(new Pair( "Food"、 "10000")); set.add(new Pair( "Hungry"、 "5000")); set.add(new Pair( "Later"、 "6000")); set.add(new Pair( "self"、 "1000")); for(iterator i = set.iterator(); i.hasnext();)system.out.println(i.next()); }} classペア実装comparable {private final string name;プライベートファイナルINT番号。 public Pair(string name、int number){this.name = name; this.number = number; } public Pair(string name、string number)throws numberformatexception {this.name = name; this.number = integer.parseint(number); } public int compareto(object o){if(o instanceof pair){int cmp = double.compare(number、((pair)o).number); if(cmp!= 0){return cmp; } return name.comPareTo((((pair)o).name); } new ClassCastException( "ペアと比較できない" + o.getClass()。getName()); } public string toString(){return name + '' + number; }}同様のC ++コード:
typedef pair <string、int> pair; int cmp(const pair&x、const pair&y){return x.second> y.second;} map <string、int> m; vector <paile> vec; curr-> second));} sort(vec.begin()、vec.end()、cmp);上記の方法の本質的な重要性は、マップ構造のキー値ペア(map.entry)をカスタムクラス(struct)にカプセル化するか、map.entryクラスを直接使用します。カスタムクラスは、自分自身を並べ替える方法、つまり値でソートする方法を知っています。比較可能なインターフェイスを具体的に実装するか、自分でコンパレータオブジェクトを構築し、マップ構造を使用せずに順序付けられたセットを使用します(sortedset、treesetはsortedsetの実装です)。つまり、Map.Entryを使用する代わりに、オブジェクトと見なされるため、オブジェクトの順序付けられたセットを実装したり、オブジェクトのセットをソートしたりすることになります。 sortedsetを使用して、挿入が完了した後に自然に注文するか、リストまたは配列を使用してから(collections.sort()またはarrays.sort())並べ替えます。
独自のクラスの情報をカプセル化します。どちらかを実装します
自然な注文のための同等のルールを書くか、書くこと
あなたの基準に基づくコンパレーター。情報をソートに保存します
収集、またはcollections.sort()メソッドを使用します。
方法2:
次のコードを使用して、値でソートすることもできます。
public static Map sortbyvalue(マップマップ){リストリスト= new linkedlist(map.entryset()); collections.sort(list、new Comparator(){public int compare(object o1、object o2){return((campleable)((map.entry)(o1))。getValue().compareto((map.entry)(o2))。getValue();}}});マップresult = new linkedhashmap(); for(iterator it = list.iterator(); it.hasnext();){map.entry entry =(map.entry)it.next(); result.put(entry.getKey()、entry.getValue()); } return result; } public static Map sortbyvalue(マップマップ、最終的なブール逆逆){リストリスト= new linkedlist(map.entryset()); collections.sort(list、new Comparator(){public int compare(object o1、object o2){if(revern){return-((comparable)((map.entry)(o1))。getValue()).compareto((map.entry)(o2))。 .compareto((map.entry)(o2))。getValue()}});マップresult = new linkedhashmap(); for(iterator it = list.iterator(); it.hasnext();){map.entry entry =(map.entry)it.next(); result.put(entry.getKey()、entry.getValue()); } return result; }マップマップ= new Hashmap(); map.put( "a"、4); map.put( "b"、1); map.put( "c"、3); map.put( "d"、2);マップsorted = sortbyvalue(map); System.out.println(sorted); // output:{b = 1、d = 2、c = 3、a = 4}または次のように実行できます。マップマップ= new Hashmap(); map.put( "a"、4); map.put( "b"、1); map.put( "c"、3); map.put( "d"、2); <map.entry <string、integer >> treeset = new treeset <map.entry <string、integer >>(new Comparator <map.entry <string、integer >>(){public compare <string、integer> o1、map.entry <string、integer> o2){integer d1 = o1.getValue(); int r = d2.compareto(d1); treeset.addall(map.entryset()); System.out.println(treeset); //出力:[a = 4、c = 3、d = 2、b = 1]さらに、Groovyで値別にソートマップを実装することはもちろんエッセンスが同じですが、非常に簡単です:
Groovyでマップの種類の方法を使用します(Groovy 1.6が必要です)。
def result = map.sort(){a、b-> b.value.compareto(a.value)}のように:
["a":3、 "b":1、 "c":4、 "d":2] .sort {a、b-> a.value -b.value}
結果は次のとおりです。[b:1、d:2、a:3、c:4]
Pythonで同様:
h = {"a":2、 "b":1、 "c":3} i = h.items()// i = [( 'a'、2)、( 'c'、3)、( 'b'、1)] i.sort(k1、v1)、(k2、v2):(k2、v2):cmp(v2、v2、v2、v2、v2、 '' '' '' '' 'n ( 'b'、1)]上記の記事では、ValueによるJavaマップソートについて簡単に説明しています。私があなたと共有するすべてのコンテンツです。私はそれがあなたに参照を与えることができることを願っています、そしてあなたがwulin.comをもっとサポートできることを願っています。