public Set<TagKey<?>> getTagPool(V v) {
return this.entrySet().stream()
.filter(entry -> entry.getValue().contains(v))
.<TagKey<?>>mapMulti((entry, consumer) -> {
if (v.getClass() == Item.class) {
consumer.accept(TagUtils.getItemTagKeyFromResource(entry.getKey()));
} else {
consumer.accept(TagUtils.getBlockTagKeyFromResource(entry.getKey()));
}
})
.collect(Collectors.toSet());
}```
Whereas `V` is declared in the class containing this method as a type parameter, would this method work as intended? Namely:
- The `consumer` is accepting instances of class `TagKey<V>`.
- The method returns a `Set<TagKey<V>>` where `V` is passed in as an argument of `#getTagPool`.
- `v.getClass() == Item.class` is a valid statement
- `#getItemTagKeyFromResource` and `#getBlockTagKeyFromResource` returns a `TagKey<V>` of respective type
#Behaviour of generics wrt my code
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @latent mist! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Also, would there a better way to accomplish what I'm trying to with this method?
Dude, we aren't going to guess what you're doing
But anyway, V is a type, so it cannot be passed as parameter
I've provided as much detail as I could think to. If you have questions wrt what I'm tying to accomplish then ask.
im gonna cut the code down a bit so i can try and illustrate what im trying to do better
public Set<TagKey<?>> getTagPool(V v) //V is defined as a type parameter in this class {
return (v.class == Item.class) ? Static.util1(v) : Static.util2(v);
}
//The goal of this method is to resolve whether the object passed as the argument is of class Item, then return a value from a static method whose type is either TagKey<Item> if the the statement is true, or TagKey<Block> if false.
//The method works as intended if this method can return as either Set<TagKey<Item>> or Set<TagKey<Block>>.
I'm unsure how to use wildcards and generics in general