简体   繁体   English

无法创建类型为Map.Entry的通用类

[英]Cannot Create a Generic class of type Map.Entry

I have a map and I want to make a Collector implementation that would take from map, an entry set, and set the keys / values inside another custom POJO. 我有一个地图,我想制作一个Collector实现,该实现将从地图中获取一个条目集,并在另一个自定义POJO中设置键/值。 I thought what would be simple was to to make a generic class of type Map.Entry , but that doesnt compile. 我以为会很简单,就是创建类型为Map.Entry的泛型类,但这不会编译。 How can I do that? 我怎样才能做到这一点?

I could rethink and design the solution in another way, but I would like to find out how to make a generic class of type an inner interface of another class, like this example. 我可以用另一种方式重新思考和设计解决方案,但是我想了解如何使类型的泛型类成为另一个类的内部接口,例如本示例。

public class TestCollectorImpl<Map.Entry> implements Collector<Map.Entry, CustomDataType, CustomDataType>{}

Looks like I cannot use the . 看来我无法使用. in the <> and this does not compile. <> ,并且不会编译。

I think this is what you want: 我认为这是您想要的:

public class TestCollectorImpl<K, V> implements Collector<Map.Entry<K, V>, Foo, Bar>

Keep in mind that TestCollectorImpl<Map.Entry> is trying to define a new generic type parameter with the name Map.Entry (which is an invalid identifier due to the dot). 请记住, TestCollectorImpl<Map.Entry>试图定义一个新的通用类型参数,名称为Map.Entry (由于点的原因,它是无效的标识符)。 You're also using the raw type of Map.Entry here: Collector<Map.Entry ... . 您还在此处使用Map.Entry原始类型Collector<Map.Entry ... You don't want to do either of these. 您不想做任何一个。

What you do want is two generic type parameters to pass along to be used as the types of the key and value of the entry. 想要的是传递两个通用类型参数,以用作键的类型和条目的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM