将Flat Collection映射到层次结构的最佳方法

时间:2017-12-29 06:57:29

标签: java collections mapping

我的API需要读取大型记录集并将其转换为层次结构(JSON),以便UI(Angular)可以正确显示它。我正在寻找一种有效的方法来实现这种转变(对于1000个记录)。

哪种收藏类型最适合?有没有首选地图制作者?

详细说明:

public class Batch implements Serializable {
    private Timestamp deliveryDateTime;
    private String deliveryLocation;
    private String patientName;
    // other batch details
}

我有一批批次Collection<Batch>。当我将此集合返回到UI时,需要首先按deliveryDateTime排序,然后按deliveryLocation排序,然后按patientName排序。

生成的JSON将如下所示:

{
    "deliveryDateTimes": [
        {
            "deliveryDateTime": "Mon, 20-Nov-2017",
            "deliveryLocations": [
                {
                    "deliveryLocation": "location1",
                    "patients": [
                        {
                            "patientName": "LastName1, FirstName1",
                            "batches": [
                                {
                                    "otherBatchDetails": "other batch details"
                                    // other batch details.
                                },
                                {
                                    "otherBatchDetails": "other batch details"
                                    // other batch details.
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:2)

你可以尝试这个。我试过了,它对我来说很好。

public class BatchTest {

    public static void main(String[] args) {
        List<Batch> sortedList = generateBatches().stream().
                sorted(Comparator.comparing(Batch::getDeliveryDateTime).reversed().
                        thenComparing(Comparator.comparing(Batch::getDeliveryLocation).
                        thenComparing(Comparator.comparing(Batch::getPatientName)))).collect(Collectors.toList());
        Map<Date, Map<String, Map<String, List<Batch>>>> result = sortedList.stream().
                collect(Collectors.groupingBy(Batch::getDeliveryDateTime,
                        Collectors.groupingBy(Batch::getDeliveryLocation,
                                Collectors.groupingBy(Batch::getPatientName,
                                        Collectors.toList()))));

        System.out.println("Batches : " + result);
    }

    private static List<Batch> generateBatches() {
        //DB call to fetch list of objects
}

答案 1 :(得分:0)

可以在此上下文中使用TreeSet集合。 TreeMap的比较器对象设计如下:

class BatchSorter implements Comparator<Batch>{

    @Override
    public int compare(Batch b1, Batch b2) {

        if(b1.getDeliveryDateTime().after(b2.getDeliveryDateTime())){
            return 1;
          } 
        else if(b1.getDeliveryDateTime().before(b2.getDeliveryDateTime())){
            return -1;
          }
        else{ // if 2 dates are equal
            if(b1.getDeliveryLocation().compareTo(b2.getDeliveryLocation())>0){
                return 1;
            }
            else if(b1.getDeliveryLocation().compareTo(b2.getDeliveryLocation())<0){
                return -1;
            }
            else{
                return(b1.getPatientName().compareTo(b2.getPatientName())); // If location names are equal
            }

        }

     }
}

这可以在TreeSet中使用,如下所示:

TreeSet<Batch> ts = new TreeSet<Batch>(new BatchSorter());