1

I have a set of data that I want to store in a simple variable. Basically a list of fifteen names with location coordinates. In Python I'd create a dict, something like:

locations = {'Location1': [lat1, lon1], 'Location2': [lat2, lon2], ...}

So that way I can later call the coordinates by using the location name as key:

coords = locations['Location1']

Any equivalent to do this in Java? I basically want to define these locations as a constant in my code.

EDIT after receiving the first very fast replies:

I have now added this:

private class coordinates {
    private Long lat;
    private Long lon;
    public Long getLat() {
        return lat;
    }
    public void setLat(Long lat) {
        this.lat = lat;
    }
    public Long getLon() {
        return lon;
    }
    public void setLon(Long lon) {
        this.lon = lon;
    }
}

private final static Map<String, coordinates> LOCATIONS;

The last line is of course incomplete; I'm at a loss on how to populate this thing, without using extra temporary variables to access the two setters, resulting in several lines of code for every single set of values.

Sadegh
  • 865
  • 1
  • 23
  • 47
Wouter
  • 2,623
  • 4
  • 34
  • 43

3 Answers3

3

You can use a Map in java:

Like: Map<LocationObject, CoordinateObject>

Where CoordinateObject has latitude and longitude as attributes

class CoordinateObject {
   //longitude;
   //latitude;
}
Community
  • 1
  • 1
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • I was hoping for something that would not add a complete class and a lot more clutter to my program. And how to populate this map? – Wouter Mar 13 '13 at 17:00
  • http://stackoverflow.com/questions/120283/working-with-latitude-longitude-values-in-java might be helpful then. – PermGenError Mar 13 '13 at 17:01
  • Not applicable in this case; I just need to store the numbers so later I can retrieve them to put markers on a map, no need for any calculations. – Wouter Mar 13 '13 at 17:08
  • @Wouter in that case using Map with Custom Coordinates class is a way to go. – PermGenError Mar 13 '13 at 17:09
3

In Java, the correct data structure for this would be a Map.

Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77
0

I think I found a more convenient solution by adapting some other answers on StackOverflow, without the trouble of having to create a separate object (and realised I was using the wrong type):

private final static Map<String, Double[]> LOCATIONS;
static
{
    LOCATIONS = new Map<String, Double[]>();
    LOCATIONS.put("Central_Western", new Double[] {22.288262, 114.140138});
    LOCATIONS.put("Eastern", new Double[] {22.286078, 114.216957});
}

Now I should be able to call Double[] loc = LOCATIONS["Central_Western"] and have an array of two numbers.

OK haven't tested it yet... trying such little things is so hard in Java/Android... I so miss Python's console...

Wouter
  • 2,623
  • 4
  • 34
  • 43
  • hmm, the thing is what if someone accidentally populates your double array with an extra decimal number ??. java is Object Oriented. and i think you should use a class body for the co-ordinates. :) – PermGenError Mar 13 '13 at 17:32
  • That is impossible as it is a static map, and all data is hard-coded. It is meant to be read-only. And this way it's reasonably clutter-free, and it works. Furthermore no clue on how to easily populate (and read) the data in the map if the value is a custom object. – Wouter Mar 13 '13 at 18:03