Search code examples
javalambdajava-streamcollectors

Stream collector error on filtering the custom list


List<RuleData> ruleDataList = conflictRuleIds.stream()
                .filter(conflictRuleId -> idsMap.containsKey(conflictRuleId))
                .collect(Collectors.toList());

This is giving me the error

no instance(s) of type variable(s) exist so that String conforms to RuleData inference variable T has incompatible bounds: equality constraints: RuleData lower bounds: String

Pricesly , i want to do is to filter the conflicRuleIds list and then check whether are in map or not.and finally collect to list

idsMap contains the key->value as [string,RuleData] and conflictingRuleIds is a list of string

For each matching Ids in the map, i have to add the value for corresponding key in the map..something like .map() function


Solution

  • You need to use map() to transform the data from id of string to RuleData by getting from idsMap.

    List<RuleData> ruleDataList = conflictRuleIds.stream()
                    .filter(conflictRuleId -> idsMap.containsKey(conflictRuleId))
                    .map(conflictRuleId -> idsMap.get(conflictRuleId))
                    .collect(Collectors.toList());