3

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.

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

2
  • what is the problem exactly? Commented Jan 1, 2013 at 13:38
  • System.out.println(me.getKey()+"value>>"+me.getValue()); here iwant to the lat and lng value by calling getLat() and getLng() method. how do i do that using me.getValue()... Commented Jan 1, 2013 at 13:42

6 Answers 6

5

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)

Location locobj = me.getValue()
locobj.getLat() // will give you latitude
locobj.getLng() // will give you longitude
Sign up to request clarification or add additional context in comments.

Comments

4

Why not just cast the value?

Location locobj = (Location)me.getValue();
locobj.getLat();
locobj.getLng();

Comments

1

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

Comments

0

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:

System.out.println(me.getKey()+"value>>"+((Location)me.getValue()).getLng()); 

Comments

0

You are retrieving an Object with getKey() or getValue(). You need to now call the getter methods to print the appropriate values.

Comments

0

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");
}

Comments

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.