0

I have a map and this contains objects, lets say of type 'Apple'. I currently have an iterator but the only thing I seem to be able to call is the getKey and getValue. getValue seems to return the memory address of the actual Apple object, but i wanted to be able to do Apple a = Map.Entry.getValue()

i can only seem to get the key and memory address :s

Iterator it = ApplesMap.entrySet().iterator();     
while (it.hasNext()) {         
    Map.Entry entries = (Map.Entry)it.next();         
    System.out.println(entries.getKey() + " = " + entries.getValue());
    //Apple a = entries.getValue(); 
} 
7
  • "i can only seem to get the key and memory address :s" :) Commented Apr 29, 2011 at 13:30
  • 2
    If your map contains entries from some key to an instance of apple, entry.getValue() returns the actual apple. If your print statement shows something like an address in memory, your Apple class should override toString() to print something meaningful. Commented Apr 29, 2011 at 13:30
  • You should use generics. Commented Apr 29, 2011 at 13:31
  • It's not the memory address, but the hashcode of that object. Commented Apr 29, 2011 at 13:31
  • @asgs from the Object.hashCode() javadocs: This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language Commented Apr 29, 2011 at 13:37

4 Answers 4

2

Try it this way if you're using JDK 6:

for (Apple apple : applesMap.values())
{
    // process apple here.
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is this a 'lesser' method than using iterators? Why do we need iterators?
@James This is a shortcut that uses an iterator internally, it's possible for all Objects that implement Iterableand it's explained here
@James yes this is called a for-each loop introduced in JDK 1.5 and iterators are an older way to achieve what you've done.
1

Try casting the value

Apple a = (Apple) entries.getValue();
a.beDelicious();

7 Comments

All other answers are better than this one, yet this gets the only upvote? Upvoting all others for balance...
@Sean feel free to downvote if the quality of answer is bad. I'll take it like a man.
@Bala R - the answer is correct. So no downvote. But others are better :)
@James - because it is more verbose and it is not compile-time safe.
@Bala no, this is not a bad answer. You are directly answering the question, correctly. But all other answers adress the problems in the question itself :-)
|
1

This is ancient code. Use Generics and for-each loops.

Map<String, Apple> map = // create map here
for(Map.Entry<String, Apple> entry : map.entrySet()){
    Apple thisIsAnApple = entry.getValue();
    String andThisIsTheKeyThatLinksToIt = entry.getKey();
}

BTW:

  • if you just want the keys, use map.keySet()
  • if you just want the values, use map.values()
  • use map.entrySet() only if you need the complete mapping

1 Comment

but you can't upvote your own.. so a little balance from me :)
1

Use generics:

Map<String, Apple> map = ....;

if you need the key:

for (Map.Entry<String, Apple> entry : map.entrySet()) {..}

if you don't need the key:

for (Apple apple : map.values()) {..}

Since you had a sub-question in one of the comments: Under the hood the for-each loop uses the Iterator. Every Iterable class is eligible for a for-each loop. But you are not bothered with operating the iterator - it is operated automatically for you. An Iterator is still useful if you want to call remove() on it.

2 Comments

Can you just clarify, why is this generics? I thought on generics you use 'E' and 'T' notation etc? :)
@James - you use E and T in the declarations (Map<K,V> for example). But when using them, you use types.

Your Answer

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.