I have a Map<Double, List<String>>
that contains the following elements:
{1.0=[AP], 2.2=[AB], 2.5=[AC], 3.5=[AD], 4.5=[AE, AF], 6.30000000000001=[AG]}
I wish to add these elements into a List so that it would print out like so or print them out like so:
[AP 1.0, AB 2.2, AC 2.5, AD 3.5 , AE 4.5, AF 4.5, AG 6.3]
How would I go about doing this?
I have tried the following
Map<Double, List<String>> mapToPopulate = new TreeMap<Double, List<String>>();
List <String> output = new ArrayList<>();
// after populating map
for(Double d: mapToPopulate .keySet()) {
result.add(mapToPopulate.get(d) + " " + d);}
but it prints it out in this form
[[AP] 1.0,[AB] 2.2,[AC] 2.5,[AD] 3.5, [AE, AF] 4.5,[AG] 6.30000000000001]
I notice, that here you are directly fetching the list and appending the value to it, and storing it in the list.
After populating the map, you can use the following code to generate the result that you expect.
// After populating map
List<String> result = mapToPopulate.entrySet()
.stream()
.flatMap(en -> en.getValue().stream().map(v -> v + " " + en.getKey()))
.collect(Collectors.toList());