我有一个HashMap,其中id是键,而Entity是值。我需要使用一个实体的属性作为键来创建一个新的HashMap,而整个实体仍然是一个值。所以我写道:
Stream<Link> linkStream = linkMap.values().stream();
HashMap<String, Link> anotherLinkMap = linkStream.collect(Collectors.toMap(l -> l.getLink(), l -> l));
但是编译器说:
Required type:
HashMap<String, Link>
Provided:
Map<Object, Object>
no instance(s) of type variable(s) K, U exist so that Map<K, U> conforms to HashMap<String, Link>
是,很容易使用for
循环编写它,但是我想使用流。我在这里做错了什么?