0

I am getting value from treemap :

I had added to hashmap all coordinates in string format which was an rraylist now its string in treemap .So I would like to retrieve all the value from the String idlatlng which contains arraylist inside the string.:

Code added to hashmap

LatLng allLatLng= new LatLng((lat1),(long1));
all.add(allLatLng);

 map.put("latlng" , all.toString());

the second treemap:

      Collections.sort(list, new Comparator<Map<String, String>>() {

            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                String value1 =  o1.get("amount");
                String value2 =  o2.get("amount");
                return Integer.parseInt(value1)-Integer.parseInt(value2);
            }
        });

        for (Map<String, String> map1 : list) {
            String id = map1.get("id");
            String amount = map1.get("amount");
            System.out.println("amount= "+amount + " , " +"id = "+id);
  String idlatlng = map1.get("latlng");
        }

In the above code I am getting the value of latlng.

String idlatlng = map1.get("latlng");

idlatlng contains arraylist of .So how do I convert the value from string above to arraylist.Its a follow up question from previous question.I really appreciate any help.Yhanks in Advance.

Output:

String idlatlng ---> arraylist<LatLng>

Unable to sort the the list by ascending order

The value I get from idlatlng is : [lat/lng: (70.0187, -141.0205), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163), lat/lng: (70.4515,-144.8163)]

Community
  • 1
  • 1
jason
  • 3,932
  • 11
  • 52
  • 123

2 Answers2

0

I think you want this:

private ArrayList<LatLng> latlngs = new ArrayList<LatLng>();

for (Map<String, String> map1 : list) {
String id = map1.get("id");
     String amount = map1.get("amount");              
     String idlatlng = map1.get("latlng");

     LatLng latLng = new LatLng(id , amount, idlatlng);

     latlngs.add(latLng);

}

Remi
  • 1,289
  • 1
  • 18
  • 52
  • Hello Remi its giving an error .Its asking me to change string idlatlng to LatLng idlatlng ? – jason Oct 09 '13 at 11:05
  • I got this error ::::::::: The constructor LatLng(String) is undefined :::::::at the line ::::: new LatLng(idlatlng); – jason Oct 09 '13 at 11:22
  • Yes you need to add those 3 Strings (id, amount, idlatlng ) to your class LatLng. So in your LatLng.class your method needs to be: public LatLng(String id, String amount, String idlatlng){ this.id = id; this.amount = amount; this.idlatlng = idlatlng; } – Remi Oct 09 '13 at 11:30
0
 LatLng allLatLng= new LatLng((lat1),(long1));
 all.add(allLatLng);
 map.put("latlng" , all.toString());

Replace above code with this one

 LatLng allLatLng= new LatLng((lat1),(long1));
 all.add(allLatLng);    
 JSONArray jsonA = JSONArray.fromObject(all);
 map.put("latlng" , jsonA.toString());

Your iteration would look like below

  for (Map<String, String> map1 : list) {
       String id = map1.get("id");
       String amount = map1.get("amount");
       System.out.println("amount= "+amount + " , " +"id = "+id);
       JSONArray jsonArray =  JSONArray.fromObject( map1.get("lanlat"));
       Collection<LatLng> collection = JSONArray.toCollection(jsonArray, LatLng.class);                       
       for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
           LatLng latLng = (LatLng) iterator.next();
           //Do whatever with your latLng object            
       }
  }

Note: To work properly LatLng class should have defualt constructor.

if you need more details let me know.........

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64