GMX GMX - 3 months ago 13
Android Question

How handle this type of data into Array - JAVA

In my app i have a function that give me that type of data:

a Year(int) and a String associated to that year (years can have associated more string):

2016 - "1A2B3C"

2009 - "8DF56R"

2009 - "8E4I1J"

2013 - "P6F2T8"

2009 - "4K6J5U"

I want create an array of String for each Year, than sort the Years for increasing years. Like this:

2009 - "8DF56R" "8E4I1J" "4K6J5U"

2013 - "P6F2T8"

2016 - "1A2B3C"

The Year should be the index,
How can i retrieve that output?

Sorry for the noob question but I tried array of array, ArrayList and Map but i'm probably missing something.


You should use a Map of Lists like this:

Map<Integer, List<String>> = new TreeMap<>();

and you can fill up the Lists associated with an Integer key (date). The TreeMap sorts your keys according to their natural ordering (ascending by default if I recall it right).

You can also create a custom Comparator if he default is not satisfactory for you