如何将选择查询的结果保存到按ID排序的几个LIST Java中?

时间:2016-05-09 08:18:33

标签: java list

我想将查询结果保存在按ID分组的多个Java列表中。

这是我的要求的结果:

image

我希望Java列表如下:List 1 (id = 1, latitude, longitude) / List2 (id = 2, latitude, longitude) ......

这是我不完整的代码:

public String  getDistanceTraveled() {

    String f = "" ;
    double distance = 0;
    allDeviceList = (ArrayList<Device>) deviceBean.getListalldevice();// Get list of all device

    List<Eventdata> list = deviceBean.getLatAndLongDeviceId();// Get list of all 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; i < 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 This distance for 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()+"");

                        }
                    }
        }

1 个答案:

答案 0 :(得分:0)

您可以使用MultiValuedMap from Apache Commons Collection来简化代码:

List<Eventdata> list = deviceBean.getLatAndLongDeviceId();
MultiValuedMap<String, Eventdata> map= new ArrayListValuedHashMap<String, Eventdata>();
for(Eventdata eventData : list) { 
    map.put(eventData.getDevice().getDeviceid(), eventData);
}

或者更好的是,如果您的项目是Java 8:

Map<String, List<Eventdata>> map = list.stream()
        .collect(Collectors.groupingBy(eventData-> eventData.getDevice().getDeviceid());