1

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?

asked Jul 9, 2013 at 20:25
10
  • Which symbol can it not find? Commented Jul 9, 2013 at 20:27
  • how is your hashTable definition? Commented Jul 9, 2013 at 20:27
  • Put a generic argument in your Iterator declaration for the type you want, ie. Iterator<ArrayList<String>>. Commented Jul 9, 2013 at 20:28
  • Well you are calling get on an Object which is where the compilation error comes from. Commented Jul 9, 2013 at 20:28
  • 1
    @gran_profaci it'd be a far better idea to just use the id as key, not the whole row. See my answer. Commented Jul 9, 2013 at 21:27

4 Answers 4

2

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.

answered Jul 9, 2013 at 20:29

Comments

1

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.

answered Jul 9, 2013 at 20:30

Comments

0

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.

answered Jul 9, 2013 at 20:42

Comments

0

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));
}
answered Jul 9, 2013 at 20:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.