1
可能性の重複:少なくとも私は含ま簡単なテストケースにそれが動作Javaでの値でマップをソート
ArrayList<Map.Entry<Integer, Integer>> suits = new ArrayList<Map.Entry<Integer, Integer>>(); suits.add(new AbstractMap.SimpleEntry(0, 3)); suits.add(new AbstractMap.SimpleEntry(1, 5)); suits.add(new AbstractMap.SimpleEntry(2, 1)); suits.add(new AbstractMap.SimpleEntry(3, 3)); Collections.sort(suits, new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Entry<Integer, Integer> e1, Entry<Integer, Integer> e2) { if (e1.getValue() > e2.getValue()) return 1; else if (e1.getValue() < e2.getValue()) return -1; else if (e1.getKey() > e2.getKey()) return 1; else if (e1.getKey() < e2.getKey()) return -1; else return 0; } });
(:ここ
How to sort a Map<Key, Value> on the values in Java?は私の仕業です)。カードの枚数を3枚(例えば3枚、5枚、5枚、6枚)順に並べ替えることを意図しています。
私はマップを値で並べ替えるというこの問題を頻繁に見ています。私はコーディングやJavaでの正式な訓練を全くしていないので、少し回答がわかりませんでした。私自身。 このコードで何か大したことはありませんか?それが毎日のWTFの価値がないなら、それは私のために十分です。
ありがとうございました。
コードには何も間違っていません。しかし、 'Map'ではそれほど多くする必要はありません。 'Map'ではなく' Map.Entry'オブジェクトとしてのキーと値のペアの 'List'を持っています。 – Jesper
カードを表すカスタムクラスを作成すると、コードを読みやすくなります。 – Mitch