My question is a little bit different. I have an ArrayList which contains some routes. For ex:
[ACD, CDE, DEB, EBJ, BJK, JKO, ACD, CDE, DEX, EXB, XBJ, BJK, JKO, KOL]
When I use HashMap for counting, it only prints me one string:
Most common route: ACD
This route repeats 2 times.
That is correct, BUT, the strings CDE, BJK and JKO also repeats 2 times. As I'm a beginner in programming, would you be so kind telling me what do I have to change in my code so it prints all the most common routes (strings).
Here's the code:
Map<String, Integer> stringsCount = new HashMap<>();
for(String string: listaRuta)
{
Integer count = stringsCount.get(string);
if(count == null) count = new Integer(0);
count++;
stringsCount.put(string,count);
}
Map.Entry<String,Integer> mostRepeated = null;
for(Map.Entry<String, Integer> e: stringsCount.entrySet())
{
if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
mostRepeated = e;
}
if(mostRepeated != null)
System.out.println("Most common route: " + mostRepeated.getKey());
System.out.println("This route repeats " + mostRepeated.getValue() + " times.");