how to use collection factory method in intelliJ with java 8

Viewed 78

as the question title, I want to use collection factory method such as 'Map.of("string", Object) in java 8 on intelliJ IDEA(Community Version).

I'm aware that it supports in java 9 version, but I've seen that using collection factory method is possible via maven dependency of 'org.codehaus.groovy' in pom.xml on eclipse.

For this reason, I assume that it is also possible to use collection factory method on intelliJ with gradle in a way.

If there is to do any for this, please give me answers.

Thanks for reading.

1 Answers

If you want to the functionality of classes added to Java 9, choose from:

  • Use Java 9
  • Use similar functionality provided in your version of Java
  • Add a library providing similar functionality

You ruled out the first.

The second approach would be the use of Collections.unmodifiableMap to produce an unmodifiable map as produced by Map.of.

Map< String , Object > mapModifiable = new HashMap<>() ;
mapModifiable.add( "dog" , whatever ) ;
mapModifiable.add( "cat" , whatever ) ;
Map< String , Object > mapUnmodifiable = Collections.unmodifiableMap( mapModifiable ) ;

The third approach requires finding and choosing a third-party library. There are at least two offering nonmodifiable maps as produced by Map.of. They may also support the convenient literal syntax provided by Map.of.

Apache Commons Collections may be a third, but I am not familiar.

Related