I am currently told to count all .sql files which are hangin' out on some servers. Manually solving this very basic task is not an option, instead, I wrote some code that makes use of a SimpleFileVisitor<Path>
and stores all the sql files found along with its parent path in a Map<Path, List<Path>>
.
Now I want to receive the total amount of sql files found independent from their locations. I got it working with an enhanced for
loop (almost the classic way):
public int getTotalAmountOfSqlFiles(Map<Path, List<Path>> sqlFilesInDirectories) {
int totalAmount = 0;
for (Path directory : sqlFilesInDirectories.keySet()) {
List<Path> sqlFiles = sqlFilesInDirectories.get(directory);
totalAmount += sqlFiles.size();
}
return totalAmount;
}
The question is now, how can I do the same using the stream API?
I wasn't able to get compilable code to work which isn't obviously doing the wrong thing.
The following line looks like a good idea to me, but not to the compiler, unfortunately.
totalAmount = sqlFilesInDirectories.entrySet().stream().map(List::size).sum();
The compiler says
Cannot infer type argument(s) for
<R> map(Function<? super T,? extends R>
Does anyone know what I am doing wrong (and maybe provide some educated stream-API using solution)?
Refactoring is the process of restructuring code, while not changing its original functionality. The goal of refactoring is to improve internal code by making many small changes without altering the code's external behavior.
Not sure why people are involving keySet
here when the required sum is only of List size contained in values. Just sum the size of all values.
return sqlFilesInDirectories.values().stream().mapToInt(List::size).sum();
And even the for loop version should be simply this,
for (List<Path> list : sqlFilesInDirectories.values()) {
totalAmount += list.size();
}
As iterating over keyset and then getting value from map isn't really required and will not be better performance wise.
It's because in stream you're taking whole entries instead of values. This should do it:
totalAmount = sqlFilesInDirectories.values().stream().map(List::size).sum();
Other answers provide the shortest way to sum all entries, but if you need the amount of scripts per Path
you can use the following:
Map<Path, Integer> amountOfFilesForPath =
files.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.summingInt(value -> value.getValue().size())));
And you can also get the total value:
int sum = amountOfFilesForPath.values().stream().mapToInt(Integer::intValue).sum();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With