I have a HashMap of teams that I need to print out. They need to be indexed and sorted by some parameters(not relevant). Everything works fine except for the indexing part which I got working in a way but it doesn't feel right.
int i=0;
public void printTable()
{
StringBuilder sb = new StringBuilder();
i = 0;
table.keySet().stream()
.map(key -> table.get(key))
.sorted(Comparator.comparing(Team::getPoints).thenComparing(Team::goalDifference).reversed().thenComparing(Team::getName))
.forEach(team -> sb.append(String.format("%2d%s %-15s%5d%5d%5d%5d%5d\n", ++i, ".", team.getName(), team.getPlayed(),
team.getWins(), team.getDraws(), team.getLosses(), team.getPoints())));
System.out.print(sb.toString());
}
Is there a better way of doing this? I don't think Intstream would help here because I wouldn't be able to get the object from the HashMap using an index.