Reputation: 13
Newbie to Java and Android...
I am using a class to store a group of Google Map LatLng points to iterate through. Here is the class I am using to assign an Arraylist to an Object:
public class GeoPoints implements Iterable<String>, Iterator<String> {
private HashMap<String, Object> internalStorage = new HashMap<String, Object>();
private int count = 0;
public void add(String key, Object value) {
internalStorage.put(key, value);
}
public Object get(String key) {
return internalStorage.get(key);
}
@Override
public boolean hasNext() {
if (count < internalStorage.size()) {
return true;
}
return false;
}
@Override
public String next() {
if (count == internalStorage.size()) throw new IndexOutOfBoundsException();
count++;
return (String) internalStorage.keySet().toArray()[count - 1];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Iterator<String> iterator() {
return this;
}
}
I am placing the points like this:
geoPoints = new GeoPoints();
// Load GeoPoints ArrayList...
List<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(45.778754, -124.811288));
points.add(new LatLng(28.781740, -124.350814));
points.add(new LatLng(48.712660, -60.982386));
points.add(new LatLng(20.354554, -58.794457));
geoPoints.add("US_MAP", points);
To access the object I am using a for loop as follows:
for (String key : geoPoints ) {
Object obj = geoPoints.get(key);
}
At this point, I can debug and see that the Object obj contains the ArrayList I created with the four points above.
For the life of me I cannot figure out how to access these Arraylist values in order to Iterate or loop through the values.
I have tried assigning the obj to an arraylist iterator as such:
ArrayList<?> myTestItr = geoPoints.get(key);
Which does not work, I am sure for obvious reasons... :)
I am sure there is a simple answer to this (using a Iterator wildcard, etc) but I am coming up short.
Any and all help is greatly appreciated!
Thanks in advance!
Upvotes: 0
Views: 404
Reputation: 13
With thanks to all, here is the solution and implementation:
First the new class with List LatLng type:
public class GeoPoints implements Iterable<String>, Iterator<String> {
private HashMap<String, List<LatLng>> internalStorage = new HashMap<String, List<LatLng>>();
private int count = 0;
public void add(String key, List<LatLng>value) {
internalStorage.put(key, value);
}
public List<LatLng> get(String key) {
return internalStorage.get(key);
}
@Override
public boolean hasNext() {
if (count < internalStorage.size()) {
return true;
}
return false;
}
@Override
public String next() {
if (count == internalStorage.size()) throw new IndexOutOfBoundsException();
count++;
return (String) internalStorage.keySet().toArray()[count - 1];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Iterator<String> iterator() {
return this;
}
}
Adding to the object:
geoPoints = new GeoPoints();
// Load GeoPoints ArrayList...
List<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(45.778754, -124.811288));
points.add(new LatLng(28.781740, -124.350814));
points.add(new LatLng(48.712660, -60.982386));
points.add(new LatLng(20.354554, -58.794457));
geoPoints.add("US_MAP", points);
To iterate through the collection:
for (String key : geoPoints) {
List<LatLng> geoPointsXY = geoPoints.get(key);
Iterator geoPtsItr = geoPointsXY.iterator();
while (geoPtsItr.hasNext()){
LatLng oNxtLatLng = (LatLng) geoPtsItr.next();
double yt = oNxtLatLng.latitude;
double nt = oNxtLatLng.longitude;
// Verify Coords here:...
}
}
Upvotes: 0
Reputation: 782
Try to cast it programmatically:
ArrayList<?> myTestItr = (ArrayList<?>) geoPoints.get(key);
Upvotes: 0
Reputation: 1553
You have to cast the Object class to ArrayList.
Casting is simply like, Converting the object to a supported Class Type.
If its not supported, it will throw a java.lang.ClassCastException
ArrayList<?> myTestItr = (ArrayList<?>) geoPoints.get(key);
This should solve your issue.
Upvotes: 0
Reputation: 329
Have you tried declaring your map as follows:
private HashMap<String, List<LatLng>> internalStorage = new HashMap<String, List<LatLng>>();
This would mean you can only add the type List<LatLng>
but you can easily iterate over the entries.
You have to edit your two methods as well:
public void add(String key, List<LatLng>value) {
internalStorage.put(key, value);
}
public List<LatLng>get(String key) {
return internalStorage.get(key);
}
Upvotes: 1