Java: Sorting Map of Type Map<String, ArrayList<String>>

foobar

I have a Map having an String as Key and an ArrayList as Value. ArrayList is basically a list of four values linked to key somehow.

My sample data is :

String : [ArrayList<String>] 

"USA":    ["25", "5", "20", "4"]
"India":  ["15", "7", "8", "2"]
"Canada": ["29", "17", "8", "6"]

I need to sort this Map in descending order as per first value in ArrayList so it becomes like below:

"Canada": ["29", "17", "8", "6"]
"USA":    ["25", "5",  "20","4"]
"India":  ["15", "7",  "8", "2"]

I am trying below function, but its not working for ArrayList type Map value:

public class MapUtil
{
    public static <K, V extends Comparable<? super V>> Map<K, V> 
        sortByValue( Map<K, V> map )
    {
        List<Map.Entry<K, V>> list =
            new LinkedList<Map.Entry<K, V>>( map.entrySet() );
        Collections.sort( list, new Comparator<Map.Entry<K, V>>()
        {
            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
            {
                return (o1.getValue()).compareTo( o2.getValue() );
            }
        } );

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list)
        {
            result.put( entry.getKey(), entry.getValue() );
        }
        return result;
    }
}

How can I modify this function to achieve what I need?

Eran

You are comparing the entire ArrayList<String> values, but you want to compare by the first value in each ArrayList.

You should do something like :

    public int compare(Map.Entry<String, ArrayList<String>> o1, Map.Entry<String, ArrayList<String>> o2)
    {
        return (o1.getValue().get(0)).compareTo( o2.getValue().get(0) );
    }

Of course you should add null checks, in case o1.getValue() returns null or an empty list.

However, you should note that this would compare the values as Strings (in which, for example "5" > "23"), not numbers. If you want a numeric comparison, you should convert the Strings to integers, or store them as integers in the first place.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sorting map<String,String> by date(value) using stream Java

From Dev

Sorting a Map by key (date,string)

From Dev

Sorting multi-type string ArrayList in Java based on Integers

From Dev

Sorting a String ArrayList with numbers Java

From Dev

Convert ArrayList<Map<String, String>> to jsonArray

From Dev

How to convert Map<String, Set<String>> to Map<String, String[]> in Java

From Dev

convert map string string to map string list string - Java 7

From Dev

Go: Cast type fails for map[string]interface{} to map[string]string

From Dev

How can I create a map of type Map<Integer, Map<String, Integer>> in Java 8?

From Java

Convert List<String> to Map<String, String> in Java

From Dev

Convert List<String> to Map<String, String> in Java

From Dev

Getting 'Type mismatch: cannot convert from ArrayList<HashMap<String,String>> to List<Map<String,String>>' while instantiating a list of maps

From Dev

Converting a Map<String, String> to a Value type with Jedis

From Dev

Java 8 List<Foo> to Map<String, Map<String, List<String>>>

From Dev

Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java

From Dev

Itetrate Map<String, Map<String, String>> using java

From Dev

Java Map<String, Map<String, Object>> convert to String and back

From Dev

JAXB Marshall Map<Integer, ArrayList<String>>

From Dev

Create a map with key=String and value=ArrayList<Double>

From Dev

Map string arraylist getting key and value

From Dev

Create a map with key=String and value=ArrayList<Double>

From Dev

Mapping an ArrayList and another internal Map with String, Integer

From Dev

Java 8: Convert a map with string values to a list containg a different type

From Dev

Sort the values in a map of type Map<String, List<String>>

From Dev

Iterate over an ArrayList< Map<String, String>> from a Java Bean using JSTL?

From Dev

Sorting ArrayList<String[]>

From Dev

How to map String to Java class?

From Dev

Java Map String <ListString> looping

From Dev

JNA map Java String to PCWSTR

Related Related

  1. 1

    Sorting map<String,String> by date(value) using stream Java

  2. 2

    Sorting a Map by key (date,string)

  3. 3

    Sorting multi-type string ArrayList in Java based on Integers

  4. 4

    Sorting a String ArrayList with numbers Java

  5. 5

    Convert ArrayList<Map<String, String>> to jsonArray

  6. 6

    How to convert Map<String, Set<String>> to Map<String, String[]> in Java

  7. 7

    convert map string string to map string list string - Java 7

  8. 8

    Go: Cast type fails for map[string]interface{} to map[string]string

  9. 9

    How can I create a map of type Map<Integer, Map<String, Integer>> in Java 8?

  10. 10

    Convert List<String> to Map<String, String> in Java

  11. 11

    Convert List<String> to Map<String, String> in Java

  12. 12

    Getting 'Type mismatch: cannot convert from ArrayList<HashMap<String,String>> to List<Map<String,String>>' while instantiating a list of maps

  13. 13

    Converting a Map<String, String> to a Value type with Jedis

  14. 14

    Java 8 List<Foo> to Map<String, Map<String, List<String>>>

  15. 15

    Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java

  16. 16

    Itetrate Map<String, Map<String, String>> using java

  17. 17

    Java Map<String, Map<String, Object>> convert to String and back

  18. 18

    JAXB Marshall Map<Integer, ArrayList<String>>

  19. 19

    Create a map with key=String and value=ArrayList<Double>

  20. 20

    Map string arraylist getting key and value

  21. 21

    Create a map with key=String and value=ArrayList<Double>

  22. 22

    Mapping an ArrayList and another internal Map with String, Integer

  23. 23

    Java 8: Convert a map with string values to a list containg a different type

  24. 24

    Sort the values in a map of type Map<String, List<String>>

  25. 25

    Iterate over an ArrayList< Map<String, String>> from a Java Bean using JSTL?

  26. 26

    Sorting ArrayList<String[]>

  27. 27

    How to map String to Java class?

  28. 28

    Java Map String <ListString> looping

  29. 29

    JNA map Java String to PCWSTR

HotTag

Archive