I am writing a method which takes an input Map
of the form Map<Term, List<Integer>>
where a Term
is defined here.
Method:
Map
and filter them using a Term
attribute. min(List.size(), 5)
) and add the output to a global var (say, totalSum
)totalSum
This is what I have written so far:
inputMap
.entrySet()
.stream()
.filter(entry -> entry.getKey().field().equals(fieldName)) // Keep only terms with fieldName
.forEach(entry -> entry.getValue()
.map(size -> Math.min(entry.getValue().size(), 5))) // These 2 lines do not work
.sum();
I am unable to take as input a stream of lists, output an integer for each of those lists and return the sum of all the outputs.
I can obviously write it using for loops, but I am trying to learn Java 8 and was curious if this problem is solvable using it.
Once we have the Stream of Integer, we can apply maths to find out even numbers. We passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 , then we would have called filter before map. That's all about how to use map and filter in Java 8.
filter() A more modern way to filter maps would be leveraging the Stream API from Java 8, which makes this process much more readable. The filter() method of the Stream class, as the name suggests, filters any Collection based on a given condition.
Filter Map Elements using Java Streams Firstly, we created a stream of our Map#Entry instances and used filter() to select only the Entries where person name starts with 'J'. Lastly, we used Collectors#toMap() to collect the filtered entry elements in the form of a new Map.
Filter takes a predicate as an argument so basically you are validating your input/collection against a condition, whereas a map allows you to define or use a existing function on the stream eg you can apply String. toUpperCase(...) etc. and transform your inputlist accordingly.
You don't need the forEach
method. You can map
each entry of the Map
to an int
, and sum
those integers :
int sum = inputMap
.entrySet()
.stream()
.filter(entry -> entry.getKey().field().equals(fieldName))
.mapToInt(entry -> Math.min(entry.getValue().size(), 5))
.sum();
With Eclipse Collections, the following will work using MutableMap and IntList.
MutableMap<Term, IntList> inputMap =
Maps.mutable.of(term1, IntLists.mutable.of(1, 2, 3),
term2, IntLists.mutable.of(4, 5, 6, 7));
long sum = inputMap
.select((term, intList) -> term.field().equals(fieldName))
.sumOfInt(intList -> Math.min(intList.size(), 5));
Note: I am a committer for Eclipse Collections.
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