How to save the results of a selection query into several LIST Java ordered by ID? -
i want save results of query in several java list grouped id.
this result of request:
i want have java list follows: list 1 (id = 1, latitude, longitude) / list2 (id = 2, latitude, longitude)
......
this incomplete code:
public string getdistancetraveled() { string f = "" ; double distance = 0; alldevicelist = (arraylist<device>) devicebean.getlistalldevice();// list of device list<eventdata> list = devicebean.getlatandlongdeviceid();// list of eventdata map<string, list<eventdata>> map= new hashmap<string, list<eventdata>>(); for(eventdata eventdata : list){ if(map.containskey(eventdata.getdevice().getdeviceid())){ list<eventdata> templist = map.get(eventdata.getdevice().getdeviceid()); templist.add(eventdata); map.put(eventdata.getdevice().getdeviceid(), templist); //system.out.println("event data device id: "+"'"+"'"+"'" +eventdata.getdevice().getdeviceid()+" "); } else{ list<eventdata> templist = new arraylist<eventdata>(); templist.add(eventdata); map.put(eventdata.getdevice().getdeviceid(), templist); //system.out.println("event data device id: "+"'"+"'"+"'" +eventdata.getdevice().getdeviceid()+" "); } } for(string deviceid: map.keyset()){ list<eventdata> eventdatalistfrommap = map.get(deviceid); if(eventdatalistfrommap.size() > 1){ for(int i=0; < eventdatalistfrommap.size()-1; i++){ eventdata div1=eventdatalistfrommap.get(i); eventdata div2 = eventdatalistfrommap.get(i+1); distance = calculatedistancebylatlong(div1.getlatitude(), div1.getlongitude(), div2.getlatitude(), div2.getlongitude()); //distance = point2d.distance(div1.getlatitude(), div1.getlongitude(), div2.getlatitude(), div2.getlongitude()); //save distance eventdata id div1.getid() } } } //system.out.println("j'affiche la distance ici"+distance+"'"+ " " +div1.getdevice().getdeviceid()+" " +div2.getdevice().getdeviceid()+""); numberformat formatter = new decimalformat("###.##"); f = formatter.format(distance); system.out.println("j'affiche la distance ici: "+"'"+ " " +f+""+ " " +div1.getdevice().getdeviceid()+" " +div2.getdevice().getdeviceid()+""); } } }
you use multivaluedmap apache commons collection in order simplify code:
list<eventdata> list = devicebean.getlatandlongdeviceid(); multivaluedmap<string, eventdata> map= new arraylistvaluedhashmap<string, eventdata>(); for(eventdata eventdata : list) { map.put(eventdata.getdevice().getdeviceid(), eventdata); }
or better, if project java 8:
map<string, list<eventdata>> map = list.stream() .collect(collectors.groupingby(eventdata-> eventdata.getdevice().getdeviceid());
Comments
Post a Comment