Reputation: 1373
For my master thesis, I'm having some problems with my Java code and I'm not really quite sure, why I am having that error message (right now)...
Basically, I have an inheritance package as shown below :
IdentifiersTable class is based on a HashMap < String, LinkedList < InfoIdentifiersTable>>.
My inheritance begins with the classe InfoIdentifiersTable.
So, the class Type,InfoType and Function extends to InfoIdentifiersTable.
Var extends to InfoType - and - Bool,Sequence,Int,Array,Set extends to Var class.
And I'm having a problem to use these inheritance with my linked list in IdentifiersTable as shown below :
Which i don't really quite understand why ...
But doing that :
That error doesn't appear anymore ... Is there any reason for that error message ?
Upvotes: 1
Views: 310
Reputation: 168051
LinkedList
has two constructors:
LinkedList()
- Constructs an empty list.LinkedList(Collection<? extends E> c)
- Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.Neither of these constructors take an individual element and instantiate the collection as you are trying to do.
If you want to populate a list in the constructor then you can use the second constructor suppling a list generated by Arrays.asList(...)
:
this.idTable.put( "integer", new LinkedList<>( Arrays.asList( integer ) ) );
(However, note that there will be a small perfomance hit as it will create an array-backed list to populate the linked list)
Upvotes: 1
Reputation: 2690
LinkedList impl. List interface and is part of Collection Framework. So it defines two constructor. Empty one and one with an initial collection.
Look here: https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#LinkedList(java.util.Collection)
But passing a single Value is not possible in the constructor and that is your only error.
Your second approach is simple and useful and I would keep it. Otherwise you would need to wrap your single "integer" value in a Collection to pass it to the LinkedList constructor, which is nonesense ;)
Upvotes: 1
Reputation: 2715
LinkedList in JDK has two constructors
LinkedList()
and
LinkedList(Collection<? extends E> c)
.
You are attempting to pass in a single element into the constructor, that's causing the error.
Upvotes: 1