如何简化对象列表到另一个列表?

时间:2018-06-12 12:13:45

标签: java

我在 Java 中有new_list = list(map(lambda x:x[:2],your_list)) ,我想将其更改为List<report>

以防

List<graph>

例如我有:

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

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

我知道有一些if(条件)和迭代它是可能的但是还有另一个解决方案吗?

任何答案都将不胜感激。

2 个答案:

答案 0 :(得分:7)

您希望每个日期有一个Graph个实例。因此,将Graph个实例存储到Map中,每个日期保留一个Graph。迭代报告列表并构建Graph(numberA,numberB,numberC)的内容,当Report个实例进入时,并根据它们携带的类型。 最后,根据地图的条目构建List<Graph>

使用Java 8(未经测试),流和collect(),假设reportsList<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)

编辑:修复编译错误, 编辑2:添加代码注释

编辑3:以上代码将“0”设置为图表中的默认值(对于给定日期/类型组合,报告不存在的情况)。我建议在Graph类中处理它(String numberA =“0”等)。否则,默认值当然是null

答案 1 :(得分:4)

我们有来自@DavidA的流解决方案。混合一个怎么样,我认为可以更清楚地认识到,结果相同:

报告类:

class Report {

    public static final String TYPE_A = "A";
    public static final String TYPE_B = "B";
    public static final String TYPE_C = "C";

    private final String date;
    private final String type;
    private final String count;
}

图表类:

class Graph {

    private final String date;

    private String numberA = "0";
    private String numberB = "0";
    private String numberC = "0";

    void addReport(Report report) {
        if (report == null || !report.getDate().equals(date))
            return;

        String type = report.getType();
        String count = report.getCount();

        if (Report.TYPE_A.equals(type ))
            numberA = count ;
        else if (Report.TYPE_B.equals(type ))
            numberB = count ;
        else if (Report.TYPE_C.equals(type ))
            numberC = count ;
    }
}

转换方法:

public static List<Graph> convertToGraphs(List<Report> reports) {
    Map<String, Graph> map = new TreeMap<>();

    reports.forEach(report -> {
        String date = report.getDate();
        Graph graph = map.get(date);

        if (graph == null)
            map.put(date, graph = new Graph(date));

        graph.addReport(report);
    });

    return new ArrayList<>(map.values());
}