Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
GMX Avatar asked Aug 11 '26 02:08

GMX


1 Answers

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

like image 195
Adam Arold Avatar answered Aug 13 '26 16:08

Adam Arold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!