2017-12-29 53 views
0

APIは大規模なレコードセットを読み込み、UI(角度)で適切に表示できるように階層構造(JSON)を変換する必要があります。私は、この変換を達成するための効率的な方法を探しています(レコード1000件分)。フラットコレクションを階層にマップする最善の方法

どのコレクションタイプが最適ですか?任意のマッパーがありますか?

詳細:

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

私はバッチCollection<Batch>のリストを持っています。このコレクションをUIに返すときは、まずdeliveryDateTime、次にdeliveryLocation、次にpatientNameでソートする必要があります。

{ 
    "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. 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 
+0

これはソートではなく、それが階層です。 – Andreas

+0

はい、どうすれば達成できますか? –

+1

JSONテキストをどのように作成するか、つまり使用するライブラリと、そのライブラリのどの機能を使用するかによって、全体的に異なります。おそらく、階層内の各ノードのクラスを作成し、それらのノードにバッチデータをマップし、次にJSONライブラリを使用してオブジェクト階層をJSONにシリアル化したいとします。 – Andreas

答えて

0

TreeSetのコレクションは、この文脈で使用することができます。

結果のJSONは次のようになります。次のように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()); 
2

あなたはこの1つを試すことができます。私は試して、それは私のために正常に動作します。

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 
} 
関連する問題