Question

While reading the source code for HashMap class I've noticed that they use (line 149)

transient Entry<K,V>[] table;

And then they initialize it with (line 283) :

table = new Entry[capacity];

And I can't see a warning related to "unchecked or unsafe operations".

But when I've tried to write something simmliar:

TestClass<T>{...
    LinkedList<T>[] arrayOfLists; ....
    arrayOfLists = new LinkedList[capacity]; //warning here

Unfortunately using @SuppressWarnings("unchecked") is not an option for me...

LinkedList<String>[]...

Still the same, so my question is what is the difference I'm missing and what would be a good solution for an array of generic collections which don't cause warnings. By the way @SuppressWarnning is unfortunately not an option for me.

Was it helpful?

Solution

The code in the Java collections api will not compile cleanly. They get warnings as well. You don't see a warning because with the JDK, you already have the .class files (or a .jar) that has the compiled code.

For your own code, just use a list of linked lists:

TestClass<T> { ...
    List<LinkedList<T>> listOfLists; ...
    listOfLists = new ArrayList<LinkedList<T>>(capacity);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top