Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Simplify List of object to another List?

Tags:

java

I have a List<report> in Java and I want to change it to List<graph>

in case that

class report {
   String date;
   String type;
   String count;
}

class graph{
   String date;
   String numberA;
   String numberB;
   String numberC;
}

for example i have :

date    type  count     change it into >  date   numberA  numberB  numberC
03/21   A     8017                        03/21   8017     5019      0
03/21   B     5019                        03/22   5001     0         8870                          
03/22   A     5001                        03/23   0        8305      0  
03/22   C     8870                        03/24   1552     2351      8221  
03/23   B     8305
03/24   A     1552                          
03/24   B     2351                          
03/24   C     8221                         

I know that with some if (condition) and iteration it can be possible but is there another solution for this?

any answer will be appreciated.

like image 203
Roozbe Avatar asked Dec 17 '22 22:12

Roozbe


1 Answers

You want one Graph instance per a date. Thus, store Graph instances into a Map that keeps one Graph per a date. Iterate over the list of reports and build the contents of Graph (numberA, numberB, numberC) as Report instances come in, and based on the type they carry. Last, build a List<Graph> based on the entries of the map.

Using Java 8 (untested), streams and collect(), assuming reports is a List<Reports>:

    List<Graph> graphs = new ArrayList<>(reports  // we want a list - so let's create one. The rest of the code is just to give it some initial contents 
                    .stream()    // stream of reports
                    .collect(HashMap<String, Graph>::new, //collect them - start with a map of date -> grap
                    (map, report)->{ // here, for each report:
                         // pick a graph instance for the date, create one if it does not exist yet
                         Graph graph = map.computeIfAbsent(report.date, date -> new Graph(report.date));   
                         // Next, populate the graph instance based on report type
                         if ("A".equals(report.type)) { graph.numberA = report.count; }  
                         else if ("B".equals(report.type)) { graph.numberB = report.count; }
                         else if ("C".equals(report.type)) { graph.numberC = report.count; }
                        },
                    Map::putAll) // see collect() javadoc for a description of why this is here
                    .values());  // last step - get all values from the map (it's a Collection)

Edit: fixed compile errors, Edit 2: added code comments

Edit 3: The above code does not set "0" as default value in the Graphs (for the case a Report does not exist for a given date / type combination). I would suggest to handle this in the Graph class ( String numberA = "0" etc.). Otherwise, the default values would be nulls of course.

like image 104
david a. Avatar answered Jan 02 '23 11:01

david a.