I have a map which represents a matrix of string-int that maps to a value:
static Map<Pair<String,Integer>, Integer> wordTopic = new HashMap<>();
I want to get the summation of values that has a specific string (a part of the pair) not the whole pair Pair<String,Integer>
AKA the summation of one row.
Example: the matrix
string/int 1 2 value1 13 26 value2 11 22
i want the summation of row values of string "value1" ..output should be =39
I have found this: Integer integerSum = wordTopic.values().stream().mapToInt(Integer::intValue).sum();
but this will get me the summation of the values of the whole key AKA the whole matrix ,i want the summation of the row only..any hints?
You can create a function as such:
int getSummationOfValuesByKey(Map<Pair<String,Integer>, Integer> map, String key) {
return map.entrySet()
.stream()
.filter(e -> e.getKey().getKey().equals(key))
.mapToInt(Map.Entry::getValue)
.sum();
}
which given a map and a key will return the summation of the values where the Pair's key is equal to the provided key.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments