简体   繁体   中英

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. I thought what would be simple was to to make a generic class of type Map.Entry , but that doesnt compile. 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). You're also using the raw type of Map.Entry here: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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