java 如何获取hashmap对象值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14110924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get hashmap object value
提问by ishk
i have a code which put object as hashmap value. I want to read lat and lng from the hashmap using iterator class. But i dont know how to do this.here is my code.
我有一个将对象作为哈希映射值的代码。我想使用迭代器类从哈希图中读取 lat 和 lng。但我不知道该怎么做。这是我的代码。
Location locobj = new Location();
HashMap loc = new HashMap();
while(rs.next()){
locobj.setLat(lat);
locobj.setLng(lon);
loc.put(location, locobj);
}
Set set = loc.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey()+"value>>"+me.getValue());
}
class location is like this
上课地点是这样的
public class Location {
private String lat;
private String lng;
private String name;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
how do i read locobj lat and lng value from getValue() method.please help
我如何从 getValue() 方法中读取 locobj lat 和 lng 值。请帮忙
回答by Manish Mulani
You should make use of generics here. Declare Map as
你应该在这里使用泛型。将地图声明为
Map<String, Location> locationMap = new HashMap<>() // assuming your key is of string type
That way, you can avoid typecasting (RTTI should be avoided - one of the design principles)
这样,您可以避免类型转换(应避免使用 RTTI - 设计原则之一)
Location locobj = me.getValue()
locobj.getLat() // will give you latitude
locobj.getLng() // will give you longitude
回答by DataNucleus
Why not just cast the value?
为什么不直接转换值?
Location locobj = (Location)me.getValue();
locobj.getLat();
locobj.getLng();
回答by Steve Atkinson
Change your code to use Generics.
更改您的代码以使用泛型。
Instead of
代替
Location locobj = new Location();
Map<Location> loc = new HashMap<Location>(); // code to interfaces, and use Generics
do
做
Location locobj = new Location();
HashMap<String,Location> loc = new HashMap<String,Location>();
and your entry as
和你的条目
Map.Entry<String,Location> me = (Map.Entry)i.next();
Then you won't have to cast anything
然后你就不必投射任何东西
回答by user3640657
The following can be used:
可以使用以下内容:
for (Entry entry : map.entrySet()) {
String key = entry.getKey();
Object values = (Object) entry.getValue();
System.out.println("Key = " + key);
System.out.println("Values = " + values + "n");
}
回答by Nikolay Kuznetsov
getValue()returns you reference to Object, but in fact object is Location. So you need to perform casting, see answer from @DataNucleus, but you can do even like this:
getValue()返回对 Object 的引用,但实际上 object 是 Location。因此,您需要执行转换,请参阅@DataNucleus 的答案,但您甚至可以这样做:
System.out.println(me.getKey()+"value>>"+((Location)me.getValue()).getLng());
回答by Kiran Mohan
You are retrieving an Object with getKey() or getValue(). You need to now call the getter methods to print the appropriate values.
您正在使用 getKey() 或 getValue() 检索对象。您现在需要调用 getter 方法来打印适当的值。