List1=[{1=one,2=two},{1=three,2=two}]
List2=[{1=one,2=twoss},{1=three,2=twoss}]
我正在尝试比较列表中的地图并以表格形式获取它。虽然我无法获得所需的html格式。所有地图中的键都是相同的,但值可以是不同的,并且尝试在屏幕和数据库之间比较标题和数据库中的键,如果它们匹配,则将用绿色包裹其他红色。所以屏幕上有两排和两排的桌子。头。数据库还有两行和标题。将这些值存储在地图列表中并进行比较。
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;
}
}
}
我想要的o / p -column header只打印一次,而现在我得到重复的html表。
答案 0 :(得分:0)
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:
<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>
修复代码中的编译时错误给了我这个输出。这是你期待的吗?