Converting HashMap to Sorted ArrayList

Dom Shahbazi

I have a HashMap<String, Integer> containing words along with their frequencies. I need to now convert this HashMap into an ArrayList of just the words, discarding of the frequencies, but i also want the ArrayList to be sorted in descending order of words by frequency.

Does anyone know an efficient way to do this?

Sergey Kalinichenko

HashMap has a convenient method called entrySet(), which lets you access a collection of key-value pairs. You can use it to construct a List<Map.Entry<String,Integer>>.

Now you have something you can sort. Use a sort method with a custom comparator, which orders entries with higher frequencies toward the beginning of the list.

With a sorted list in hand, all you need to do is walk it, and harvest the words which are now in the proper order.

List<Map.Entry<String,Integer>> entries = new ArrayList<Map.Entry<String,Integer>>(
    freqMap.entrySet()
);
Collections.sort(
    entries
,   new Comparator<Map.Entry<String,Integer>>() {
        public int compare(Map.Entry<String,Integer> a, Map.Entry<String,Integer> b) {
            return Integer.compare(b.getValue(), a.getValue());
        }
    }
);
for (Map.Entry<String,Integer> e : entries) {
    // This loop prints entries. You can use the same loop
    // to get the keys from entries, and add it to your target list.
    System.out.println(e.getKey()+":"+e.getValue());
}

Demo.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Converting ArrayList to HashMap, however, selecting choice objects with different variable classes within ArrayList

分類Dev

Converting JSONarray to ArrayList

分類Dev

Converting ArrayList<String> to int[]

分類Dev

Converting arraylist to JSON

分類Dev

How to add HashMap to ArrayList

分類Dev

Selecting top 'n' entries in a sorted HashMap

分類Dev

Java error when converting String in ArrayList to int

分類Dev

Reading Strings from a file to an ArrayList then converting to an Array

分類Dev

LinkedHashMapとHashMap!= LinkedListとArrayList

分類Dev

ArrayListを更新するHashMap

分類Dev

Java HashMap:arraylistへの追加

分類Dev

Thread-safe HashMap of Objects with Nested ArrayList

分類Dev

How to set ArrayList<HashMap<String, String>> in AlertDialog?

分類Dev

HashMapとArrayListの順序

分類Dev

ArrayListはHashMapで空です

分類Dev

Are the HashMap entries always sorted by key, if the key is of type Integer?

分類Dev

ArrayList <HashMap <String、String >>を別のArrayList <HashMap <String、String >>に追加します

分類Dev

HashMap の HashMap を ArrayList の ArrayList に変換する

分類Dev

HashMapのArrayListに要素を追加する方法

分類Dev

JavaのHashMapとArrayListの違いは?

分類Dev

ArrayListをHashMapに変換する

分類Dev

HashMapをArrayListに変換する

分類Dev

How do I store an ArrayList<HashMap<String, String>> in SharedPreferences?

分類Dev

ArrayListとHashMapの容量増加の違い

分類Dev

ArrayList <HashMap <String、String >>値を取得します

分類Dev

HashMAPを使用したArrayListへの文字列

分類Dev

ArrayList <HashMap <String、?>>を更新します

分類Dev

Android:how to have an Array of type ArrayList<HashMap<String, String>>

分類Dev

Hashmap <StringString>からArrayListを取得する方法

Related 関連記事

  1. 1

    Converting ArrayList to HashMap, however, selecting choice objects with different variable classes within ArrayList

  2. 2

    Converting JSONarray to ArrayList

  3. 3

    Converting ArrayList<String> to int[]

  4. 4

    Converting arraylist to JSON

  5. 5

    How to add HashMap to ArrayList

  6. 6

    Selecting top 'n' entries in a sorted HashMap

  7. 7

    Java error when converting String in ArrayList to int

  8. 8

    Reading Strings from a file to an ArrayList then converting to an Array

  9. 9

    LinkedHashMapとHashMap!= LinkedListとArrayList

  10. 10

    ArrayListを更新するHashMap

  11. 11

    Java HashMap:arraylistへの追加

  12. 12

    Thread-safe HashMap of Objects with Nested ArrayList

  13. 13

    How to set ArrayList<HashMap<String, String>> in AlertDialog?

  14. 14

    HashMapとArrayListの順序

  15. 15

    ArrayListはHashMapで空です

  16. 16

    Are the HashMap entries always sorted by key, if the key is of type Integer?

  17. 17

    ArrayList <HashMap <String、String >>を別のArrayList <HashMap <String、String >>に追加します

  18. 18

    HashMap の HashMap を ArrayList の ArrayList に変換する

  19. 19

    HashMapのArrayListに要素を追加する方法

  20. 20

    JavaのHashMapとArrayListの違いは?

  21. 21

    ArrayListをHashMapに変換する

  22. 22

    HashMapをArrayListに変換する

  23. 23

    How do I store an ArrayList<HashMap<String, String>> in SharedPreferences?

  24. 24

    ArrayListとHashMapの容量増加の違い

  25. 25

    ArrayList <HashMap <String、String >>値を取得します

  26. 26

    HashMAPを使用したArrayListへの文字列

  27. 27

    ArrayList <HashMap <String、?>>を更新します

  28. 28

    Android:how to have an Array of type ArrayList<HashMap<String, String>>

  29. 29

    Hashmap <StringString>からArrayListを取得する方法

ホットタグ

アーカイブ