N
N
Nikita072021-08-20 11:52:21
Java
Nikita07, 2021-08-20 11:52:21

How does Comparator.comparing work in Java?

Hello everyone, please help me figure out what the next line does?

.sorted(Comparator.comparing(Matcher::getSourceFileName).thenComparingLong(Matcher::getSourceFileLineNumber))


Full code
getAllMatchers()
            .stream()
            .sorted(Comparator.comparing(Matcher::getSourceFileName).thenComparingLong(Matcher::getSourceFileLineNumber))
            .forEach(matcher -> {
                MatcherImpact matcherImpact = new MatcherImpact();
                matcherImpact.name = matcher.getMatcherSourceLocation();
                impactOverview.put(matcher.getMatcherSourceLocation(), matcherImpact);
                impactList.add(matcherImpact);
            });


For reference
getSourceFileName is a string,
getSourceFileLineNumber is an int

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romank0, 2021-08-20
@romank0

The stream will first be sorted alphabetically by the value returned by `getSourceFileName`, and then (i.e. if the values ​​of `sourceFileName` are the same) will be sorted by the value of `getSourceFileLineNumber`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question