So I’ve been searching a while for the answer to this and I’m just not sure how it works.
I’m trying to make a list of BloomFilter<String> objects.
The class definition for the BloomFilter is:
public class BloomFilter<E> implements Serializable { ...
The <E> allows the user to pick what type of elements are going into the filter. In my case, I need strings.
Somewhere else in the program, I need 4 BloomFilter<String> objects.
My question is: How do I initialize the following line?
private static BloomFilter<String> threadedEncrpytionFilters[] = null;
threadedEncryptionFilters = ???
This seems similar to creating a list of ArrayLists? Is that also possible?
After seeing that someone alread answered this question I wanted to remove this answer but I see by the comments that people are still confused so here it goes 🙂
The specification clearly states, that what you want to do is illegal. Meaning:
won’t compile. You can’t create an array of concrete generic classes. When it comes to generics you can store in arrays only:
The workaround to your problem is, as already stated, to change the array to a
List<BloomFiler<String>>.This behaviour is actually pretty logical if you take into account how Java handles generic types at different stages (compile, runtime etc). After understanding that you’ll see that arrays of concrete generic types wouldn’t be type-safe. Here’s a mighty good read on this subject: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104