How to add additional methods to autocomplete in IntelliJ

Viewed 110

When I'm coding using IntelliJ I frequently use the auto-completion as a way of speeding up my coding, however there are certain methods that it doesn't seem to suggest. One example is Collectors.toMap - it will suggest toSet, toColletion and toList when I type .collect but never toMap which means I need to type more.

As a 1st question, can I fix this behaviour? As a more general question, can I add my own custom code to be auto completed in these types of circumstance?

1 Answers

One trick which helps me out every time while trying to collect the stream is having a placeholder variable with the expected type. Something like:

// just a placeholder variable with proper types, which I can remove later!
Map<String, String> tempMapVariable = someCollection.stream()
       .collect(
           // here it will suggest the .toMap(...)
           Collectors.toMap(i -> i.getKey(), i -> getValue())
       );

Set<String> tempSetVariable = someCollection.stream()
       .map(SomeCollection::mapperFn)
       .collect(
           // here it will suggest the .toSet()
           Collectors.toSet()
       );
Related