I currently have the following functional scenario:
A list of the Studios class with id and name: List<Studios> studios;
I count repeated names in List
as follows:
Map<String, Integer> counts = new HashMap<>();
studios.forEach(studio -> counts.merge(studio.getName(), 1, Integer::sum));
No Map<String, Integer> counts
has as key
the name and as value the total of repetition found in List studios
: Key = "Xpto", value = 5
.
I order the return of Map counts
this way:
result = counts.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e2, LinkedHashMap::new));
Getting the json results:
{
"Studio1": 6,
"Studio3": 5,
"Studio2": 4
}
My need is to pass a dto at the time of ordering to get the json return as follows:
{
"studios": [
{
"name": "Studio 1",
"cout": 6
},
{
"name": "Studio 2",
"count": 5
}
]
}
My Dto:
public class Dto {
private String studioName;
private Integer count;
}
Note: Open to suggestions on how to improve the code are welcome.