How to get attributes for HashMap value?
"my" code:
public void iterateHashmap2() {
HashMap hashmap = this.media;
Iterator it = hashmap.keySet().iterator();
while (it.hasN开发者_Go百科ext())
{
Object key = it.next();
Object val = hashmap.get(key);
// doesn't work..
System.out.println(val.getAttribute);
}
}
So my question is, how do I get the attributes that the value contains. The value is a class I made myself, which contains 4 Strings and another class I made, as attributes. (What I want to do is iterate through a hashmap and compare the String of an attribute that is stored in the value with input data.. so I need to be able to access the attributes in the values of the hashmap..hope that makes sense..)
You access values in a map by providing a key:
// create a map
Map<String, MyObject> map = new HashMap<String, MyObject>();
// store a value
map.put("key", someObject);
// retrieve a value
MyObject someObject2 = map.get("key");
You can also use
map.keySet()
to retrieve all keysmap.values()
to retrieve all values, without keysmap.entrySet()
to retrieve all Mappings. Each Entry represents one key mapped to one value.
a) In my code I use Java Generics, because that has been the standard way to do it for at least 5 years now.
b) You should consider reading The Map Interface from the Java Tutorial.
a): If you are using JDK1.5 or higher, please use generic and enhanced loop. The code will much simpler and safer:
for(Map.Entry<Key, YourObj> entry: this.media.entrySet()){
System.out.println(entry.getValue().getAttribute());
}
b): If you have to use JDK1.4 or lower, you have to cast your object: ((YourObj)val).getAttribute();
Object val = hashmap.get(key);
// doesn't work..
System.out.println(val.getAttribute());
The problem here is that Java is statically typed, which means you would need to cast val
in order to access non-Object methods. You need to cast val
as the type actually in the hashmap, as you're not using generics.
MyClass val = (MyClass) hashmap.get(key);
Try changing this line:
Object val = hashmap.get(key);
to this line:
YourClass val = (YourClass) hashmap.get(key);
Since you are getting an Object type you cannot just call your class's method without first casting it correctly.
Btw you should seriously consider using Java Generics
You will need a cast to a your target object's type. Like:
YourObject val = (YourObject) hashmap.get(key);
But ...
- Your Map should be generic like
Map<KeyClass, ValueClass>
- There is better way to iterate over a
Map
:
Example:
for (Map.Entry<KeyClass, ValueClass> entry : map.entrySet()) {
KeyClass key = entry.getKey();
ValueClass val = entry.getValue());
}
If your question is how to cast the value mapped to the key in the HashMap why not use generics to achieve that:
public void iterateHashmap2() {
HashMap<Object,YourClass> hashmap = this.media;
Iterator<YourClass> it = hashmap.keySet().iterator();
while (it.hasNext())
{
Object key = it.next();
YourClass val = hashmap.get(key);
// if YourClass has a getAttribute() method then this will work
System.out.println(val.getAttribute());
}
}
Also note that it's always good practice to work at interface level -- so this line:
HashMap<Object,YourClass> hashmap = this.media;
should be
Map<Object,YourClass> hashmap = this.media;
精彩评论