0

I am just a newer to groovy.

@Service
@CompileStatic
@Slf4j
class JourneyExecutionService {

    @Autowired
    List<DecisionEngineService> engineList;

    Map<String, DecisionEngineService> engineMap;
    void init(){
        engineMap = engineList.collectEntries {[it.getIndex(), it]}
        engineMap = engineList.stream().collect(Collectors.toMap(DecisionEngineService.getIndex, Functions.identity()))
    }

The compile shows both statements in the init function fail due to the error:

Cannot assign 'Map<Object, Object>' to 'List<String, DecisionEngineService>' and Cannot resolve symbol 'getIndex'

The 2nd statement in a java stream style.

The interface interface is like

interface DecisionEngineService {

    String getIndex()
}

Can anyone helps fix the compileation issue? Thanks

shijie xu
  • 1,975
  • 21
  • 52
  • Should it not be `.collectEntries {[(it.getIndex()): it]}`? `DecisionEngineService.getIndex` is unknown syntax. `DecisionEngineService::getIndex` should work in groovy 3, or alternatively, use a corresponding closure – ernest_k Jul 13 '21 at 08:44
  • @ernest_k no, he's using the list on `collectEntries{}` – injecteer Jul 13 '21 at 09:19
  • Its `DecisionEngineService::getIndex` in java and it will only work for Groovy>=3 – cfrick Jul 13 '21 at 11:47

1 Answers1

1

It should be enough to cast the map explicitly:

Map<String, DecisionEngineService> engineMap;

void init(){
  engineMap = (Map<String, DecisionEngineService>)engineList.collectEntries {[it.index, it]}
}
injecteer
  • 20,038
  • 4
  • 45
  • 89