I basically have a HashTable containing ArrayList<String>,Boolean
. I need to retrieve the key from the Hashtable. I then need to get the first value from the ArrayList<String>
which is the key.
I've tried :
Hashtable<ArrayList<String>,Boolean> tableRows = tableRead(); // returns the Hashtable.
ArrayList<String> IDs = new ArrayList<String>();
Iterator it = tableRows.keySet().iterator();
while (it.hasNext()) {
IDs.add(it.next().get(0));
}
However, this gives me an error: cannot find symbol
[javac] symbol: method get(int)
[javac] location: class Object
Just to give a functional idea: I basically have an entire DB row as a key in the Hashtable. I need to get back only the ID.
Can someone help me out with this?
4 Answers 4
You have declared a raw Iterator
, so its next()
method will return an Object
, which has no get
method. It is your key, but it's typed as Object
because your Iterator
is raw (no generic type parameter).
Use the generic Iterator
returned from the set of keys.
Iterator<ArrayList<String>> it = tableRows.keySet().iterator();
Then it.next()
will return an ArrayList<String>
, on which you can call get
.
Comments
You have to use raw types in your Iterator
Iterator<ArrayList<String>> it = tableRows.keySet().iterator();
And for recommendation, never use a mutable object as a Key cause you would have unexpected behaviour.
If an object’s hashCode() value can change based on its state, then we must be careful when using such objects as keys in hash-based collections to ensure that we don’t allow their state to change when they are being used as hash keys. All hash-based collections assume that an object’s hash value does not change while it is in use as a key in the collection. If a key’s hash code were to change while it was in a collection, some unpredictable and confusing consequences could follow. This is usually not a problem in practice — it is not common practice to use a mutable object like a List as a key in a HashTable.
If you still want make the String collection unmodifiable in this way.
List<String> unmodifiableList = Collections.unmodifiableList(myKeyList);
and use unmodifiableList
as key.
Comments
try this
ArrayList<String> list = new ArrayList<>();
Hashtable<ArrayList<String>,Boolean> tableRows = new Hashtable<>();
Set<ArrayList<String>> keys = tableRows.keySet();
Iterator<ArrayList<String>> itr = keys.iterator();
while(itr.hasNext()){
itr.next().get(0);
}
Hope this willhelp you.
Comments
Using an ArrayList<String>
as the key to a Map
is a very, very bad idea. You must never use a mutable object as a key, if by any chance the list changes the key will be invalid - this is a design smell (and a stinky one).
As an alternative, I'd suggest you build an immutable key with the concatenation of the strings in the ArrayList
, or use an immutable list created with Collections.unmodifiableList()
or even better, only use the id
column, it doesn't make any sense to use the entire row as a key.
Anyway, if you have to use an ArrayList
as key, the following code will fix the problem - and there's no need to explicitly use iterators, an enhanced for
loop is a much better alternative for iterating over the keys in this case:
for (ArrayList<String> al : tableRows.keySet()) {
IDs.add(al.get(0));
}
Iterator<ArrayList<String>>
.get
on anObject
which is where the compilation error comes from.