Hash Map Indexing with Streams

I have a HashMap command that I need to print. They need to be indexed and sorted by some parameters (not applicable). Everything works fine, except for the indexing I worked for, but that doesn't seem 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 to do this? I don't think Intstream will help here because I could not get the object from the HashMap using the index.

+4
source share
2 answers

Updated Answer

You should avoid stateful streams / lambda expressions. the link . For this reason, my initial answer below is a bad idea.

The best approach is to split the stream and print. A.

List<Team> teams = table.values()
        .stream()
        .sorted(...)
        .collect(Collectors.toList());

StringBuilder sb = new StringBuilder();
for(int i=0; i<teams.size(); i++) {

    Team team = teams.get(i);
    sb.append(String.format("%2d. %-15s%5d%5d%5d%5d%5d\n", 
            i,
            team.getName(),
            team.getPlayed(),
            team.getWins(),
            team.getDraws(),
            team.getLosses(), 
            team.getPoints()));
}

System.out.println(sb.toString());

Original answer [Bad idea]

/, . , ,

AtomicInteger i = new AtomicInteger();
String result = table.values()
        .stream()
        .sorted(...)
        .map(e -> i.incrementAndGet() + "_" + e)
        .collect(Collectors.joining());

AtomicInteger / . , .

@Eugene , AtomicInteger , .

+2

. ​​

:

List<Team> sorted = table.values().stream()
    .sorted(Comparator.comparing(Team::getPoints)
            .thenComparing(Team::goalDifference)
            .reversed()
            .thenComparing(Team::getName))
    .collect(Collectors.toList());

String table = IntStream.range(0, sorted.size())
    .mapToObj(i -> String.format("%2d%s %-15s%5d%5d%5d%5d%5d", 
            i, ".", 
            sorted.get(i).getName(), 
            sorted.get(i).getPlayed(),
            sorted.get(i).getWins(), 
            sorted.get(i).getDraws(), 
            sorted.get(i).getLosses(), 
            sorted.get(i).getPoints()))
    .collect(Collectors.joining("\n"));

, Team , :

public String formatWithPosition(int position) {
    return String.format("%2d%s %-15s%5d%5d%5d%5d%5d", 
            position, ".", 
            name, played, wins, draws, losses, points);
}

:

String table = IntStream.range(0, sorted.size())
    .mapToObj(Team::formatWithPosition)
    .collect(Collectors.joining("\n"));
+2

Source: https://habr.com/ru/post/1692650/


All Articles