Whilst trying to use Vavr's immutable map (io.vavr.collection.HashMap
) with the java.util.Map
interface, I did not manage to get the code to compile - at least not by using the .of()
static method in io.vavr.collection.HashMap
.
In essence this is the Maven dependency I am using:
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.2</version>
</dependency>
with Java 1.8
And this is the code:
import io.vavr.collection.HashMap;
import java.util.Map;
public class EntityKeyMap {
public static final Map<String, String> map =
HashMap.of("key1", "val1", "key2", "val2", "key3", "val3");
private EntityKeyMap() {
}
}
This is the error I am getting:
Incompatible types. Required Map but 'of' was inferred to HashMap: no instance(s) of type variable(s) K, V exist so that HashMap conforms to Map
Any ideas how you can assign an instance of io.vavr.collection.HashMap
to java.util.Map
? Is that even possible?
According to io.vavr.collection.HashMap
documentation it implements the java.util.Map
interface:
https://static.javadoc.io/io.vavr/vavr/0.9.2/io/vavr/collection/HashMap.html
There are some examples on the web where it seems to be possible, like on this blog where you can find this code:
Map<String, String> map1
= HashMap.of("key1", "val1", "key2", "val2", "key3", "val3");