简体   繁体   English

比较Java中的地图列表和表中的打印结果

[英]comparing list of maps in java and printing result in table

寻找这个输出 I have two list of maps. 我有两个地图清单。

 List1=[{1=one,2=two},{1=three,2=two}]
 List2=[{1=one,2=twoss},{1=three,2=twoss}]

I am trying to compare maps in lists and get it in table form html. 我正在尝试比较列表中的地图,并以表格形式html进行获取。 Though I am unable to get desired html format. 虽然我无法获得所需的html格式。 Keys in all maps will be same but values can be different and trying to get keys in header and values beneath it comparing from screen and database, if they match the it will be wrapped in green else red. 所有地图中的键都是相同的,但值可以不同,并且试图从屏幕和数据库中比较标题和其下面的值的键,如果它们匹配,则将其包裹为绿色,否则为红色。 So screen has table with two rows & header. 所以屏幕上有两行和标题的表。 Database also has two rows and header. 数据库也有两行和头。 Storing this values in list of maps and comparing them. 将此值存储在地图列表中并进行比较。

public String compare(List<Map<String, Object>> list1, List<Map<String, Object>> list2) throws Exception {
    StringBuilder tbe = new StringBuilder();
    String col = "";
    String screen = "";
    String db = "";
    if (list1.size() = list2.size()) {
        for (int i = 0; i < list1.size(); i++) {
            Map<String, Object> a = list1.get(i);
            Map<String, Object> b = list2.get(i);
            for (Map.Entry<String, Object> ent : a.entrySet()) {
                col = col + "<th>" + entry.getKey() + "</th>";
                String k = ent.getKey();
                Object o1 = entry.getValue();
                if (o1.equals(b.get(key))) {
                    screen = screen + "<td bgcolor=green>" + o1 + "</td>";
                    db = db + "<td bgcolor=green>" + b.get(entry.getKey()) + "</td>";
                } else {
                    screen = screen + "<td bgcolor=red>" + o1 + "</td>";
                    db = db + "<td bgcolor=red>" + b.get(entry.getKey()) + "</td>";
                }

            }
            tbe.append("<tr><th>col</th>" + col + "</tr>");
            tbe.append("<tr><th>screen</th>" + screen + "</tr>");
            tbe.append("<tr><th>db</th>" + db + "</tr>");
            tbe.append("</table></html>");
            String tbel = tbe.toString();
            system.out.println(tbel);
            return tbel;
        }
    }
}

Desired o/pi am looking for --column headers get printed just once, whereas now I am getting repeated html tables. 所需的o / pi正在寻找--column标头仅被打印一次,而现在我却得到了重复的html表。

    public static String compare(List<Map<String, Object>> list1, List<Map<String, Object>> list2) {
        StringBuilder tbe = new StringBuilder();
        String col = "";
        String screen = "";
        String db = "";
        if (list1.size() == list2.size()) {
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> a = list1.get(i);
                Map<String, Object> b = list2.get(i);
                for (Map.Entry<String, Object> ent : a.entrySet()) {
                    col = col + "<th>" + ent.getKey() + "</th>";
                    String key = ent.getKey();
                    Object o1 = ent.getValue();
                    if (o1.equals(b.get(key))) {
                        screen = screen + "<td bgcolor=green>" + o1 + "</td>";
                        db = db + "<td bgcolor=green>" + b.get(ent.getKey()) + "</td>";
                    } else {
                        screen = screen + "<td bgcolor=red>" + o1 + "</td>";
                        db = db + "<td bgcolor=red>" + b.get(ent.getKey()) + "</td>";
                    }

                }
                tbe.append("<tr><th>col</th>" + col + "</tr>");
                tbe.append("<tr><th>screen</th>" + screen + "</tr>");
                tbe.append("<tr><th>db</th>" + db + "</tr>");
                tbe.append("</table></html>");
            }
        }
        return tbe.toString();
    }

o/p: o / p:

<tr>
    <th>col</th>
    <th>1</th>
    <th>2</th>
</tr>
<tr>
    <th>screen</th>
    <td bgcolor=red>One</td>
    <td bgcolor=red>Two</td>
</tr>
<tr>
    <th>db</th>
    <td bgcolor=red>M2One</td>
    <td bgcolor=red>M2Two</td>
</tr>
</table>

</html>

Fixing compile time errors in your code is giving me this output. 在你的代码修复编译时错误是给我这个输出。 Is this what you are expecting? 这是您所期望的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM