优化 private void addReferencedKpi(List<AnalysisTableDto> list, List<String> codeList, List<Map<String, List<String>>> referencedList) { if (CollectionUtils.isEmpty(list) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) { return; } // 转换结构 key1-table key2-column value-codeList Map<String, Map<String, List<String>>> tableColumnWithCodeListMap = Maps.newHashMap(); int size = referencedList.size(); for (int i = 0; i < size; i++) { String code = codeList.get(i); Map<String, List<String>> referencedPerMap = referencedList.get(i); if (MapUtils.isEmpty(referencedPerMap)) { continue; } referencedPerMap.forEach((table, columns) -> { tableColumnWithCodeListMap.putIfAbsent(table, Maps.newHashMap()); for (String column : columns) { tableColumnWithCodeListMap.get(table).putIfAbsent(column, Lists.newArrayList()); tableColumnWithCodeListMap.get(table).get(column).add(code); } }); } // 装饰引用kpi for (AnalysisTableDto simulationTableDto : list) { String simTableName = simulationTableDto.getTableName(); for (SimulationColumnDto column : simulationTableDto.getColumns()) { String simTitle = column.getTitle(); if (!tableColumnWithCodeListMap.containsKey(simTableName) || !tableColumnWithCodeListMap.get(simTableName).containsKey(simTitle)) { continue; } List<String> referencedCodeList = tableColumnWithCodeListMap.get(simTableName).get(simTitle); column.setReferencedKpis(referencedCodeList); } } }
时间: 2023-12-31 08:05:19 浏览: 79
这段代码的主要功能是将一个数据结构进行转换,并将转换后的结果装饰到数据模型中。下面是一些优化建议:
1. 使用stream API来简化代码,提高可读性和可维护性。
2. 使用Optional类来避免对null进行判断。
3. 使用更具有描述性的变量名来提高可读性。
4. 将List转换为Set来提高查找效率。
下面是优化后的代码:
```
private void addReferencedKpi(List<AnalysisTableDto> tableDtoList, List<String> codeList, List<Map<String, List<String>>> referencedList) {
if (CollectionUtils.isEmpty(tableDtoList) || CollectionUtils.isEmpty(codeList) || CollectionUtils.isEmpty(referencedList)) {
return;
}
Map<String, Map<String, Set<String>>> tableColumnWithCodeMap = new HashMap<>();
IntStream.range(0, referencedList.size())
.filter(i -> MapUtils.isNotEmpty(referencedList.get(i)))
.forEach(i -> {
String code = codeList.get(i);
referencedList.get(i).forEach((table, columns) -> {
Map<String, Set<String>> columnWithCodeMap = tableColumnWithCodeMap.computeIfAbsent(table, k -> new HashMap<>());
columns.forEach(column -> columnWithCodeMap.computeIfAbsent(column, k -> new HashSet<>()).add(code));
});
});
tableDtoList.forEach(tableDto -> {
String tableName = tableDto.getTableName();
tableDto.getColumns().stream()
.filter(column -> tableColumnWithCodeMap.containsKey(tableName) && tableColumnWithCodeMap.get(tableName).containsKey(column.getTitle()))
.forEach(column -> column.setReferencedKpis(tableColumnWithCodeMap.get(tableName).get(column.getTitle())));
});
}
```
阅读全文
相关推荐














package ece448.iot_sim; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ece448.iot_sim.http_server.RequestHandler; public class HTTPCommands implements RequestHandler { // Use a map so we can search plugs by name. private final TreeMap<String, PlugSim> plugs = new TreeMap<>(); public HTTPCommands(List plugs) { for (PlugSim plug: plugs) { this.plugs.put(plug.getName(), plug); } } @Override public String handleGet(String path, Map<String, String> params) { // list all: / // do switch: /plugName?action=on|off|toggle // just report: /plugName logger.info("HTTPCmd {}: {}", path, params); if (path.equals("/")) { return listPlugs(); } PlugSim plug = plugs.get(path.substring(1)); if (plug == null) return null; // no such plug String action = params.get("action"); if (action == null) return report(plug); // P2: add your code here, modify the next line if necessary if("toggle".equals(action)){ plug.toggle(); return report(plug); }else if("on".equals(action)){ plug.switchOn(); return report(plug); }else if("off".equals(action)){ plug.switchOff(); return report(plug); } return "<html><body></body></html>"; } protected String listPlugs() { StringBuilder sb = new StringBuilder(); sb.append("<html><body>"); for (String plugName: plugs.keySet()) { sb.append(String.format("%s", plugName, plugName)); } sb.append("</body></html>"); return sb.toString(); } protected String report(PlugSim plug) { String name = plug.getName(); return String.format("<html><body>" +"Plug %s is %s." +"Power reading is %.3f." +"Switch On" +"Switch Off" +"Toggle" +"</body></html>", name, plug.isOn()? "on": "off", plug.getPower(), name, name, name); }如何对上述代码全部功能进行测试呢?请提供测试代码。



前端以该格式<input type="checkbox" class="c1"> 01(pid) 毛巾(pname) 生活用品(cname) <form action="deleteProductByPid.action">展示数据库信息,数据库有两个表,类别表category表里有cid和cname,商品表product表里有pid、pname和外键cid。该需求的前端和后端代码
分析以下html 使用这个js 在Java中实现markdown转思维导图生成html:
<html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Markmap</title> <style> * { margin: 0; padding: 0; } html { font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; } #mindmap { display: block; width: 100vw; height: 100vh; } .markmap-dark { background: #27272a; color: white; } </style> </head> <body> <svg id="mindmap"></svg> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser/index.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script> <script>(e=>{window.WebFontConfig={custom:{families:["KaTeX_AMS","KaTeX_Caligraphic:n4,n7","KaTeX_Fraktur:n4,n7","KaTeX_Main:n4,n7,i4,i7","KaTeX_Math:i4,i7","KaTeX_Script","KaTeX_SansSerif:n4,n7,i4","KaTeX_Size1","KaTeX_Size2","KaTeX_Size3","KaTeX_Size4","KaTeX_Typewriter"]},active:()=>{e().refreshHook.call()}}})(()=>window.markmap)</script><script src="https://cdn.jsdelivr.net/npm/[email protected]/webfontloader.js" defer></script><script>(()=>{setTimeout(()=>{const{markmap:S,mm:Q}=window,$=new S.Toolbar;$.attach(Q);const I=$.render();I.setAttribute("style","position:absolute;bottom:20px;right:20px"),document.body.append(I)})})()</script> <script>((l,U,M,R)=>{const N=l();window.mm=N.Markmap.create("svg#mindmap",(U||N.deriveOptions)(R),M),window.matchMedia("(prefers-color-scheme: dark)").matches&&document.documentElement.classList.add("markmap-dark")})(()=>window.markmap,null,{"content":"markmap","children":[{"content":"Links","children":[{"content":"Website","children":[],"payload":{"tag":"li","lines":"8,9"}},{"content":"GitHub","children":[],"payload":{"tag":"li","lines":"9,11"}}],"payload":{"tag":"h2","lines":"6,7"}},{"content":"Related Projects","children":[{"content":"coc-markmap for Neovim","children":[],"payload":{"tag":"li","lines":"13,14"}},{"content":"markmap-vscode for VSCode","children":[],"payload":{"tag":"li","lines":"14,15"}},{"content":"eaf-markmap for Emacs","children":[],"payload":{"tag":"li","lines":"15,17"}}],"payload":{"tag":"h2","lines":"11,12"}},{"content":"Features","children":[{"content":"Lists","children":[{"content":"strong del italic highlight","children":[],"payload":{"tag":"li","lines":"23,24"}},{"content":"inline code
","children":[],"payload":{"tag":"li","lines":"24,25"}},{"content":"<svg width=\"16\" height=\"16\" viewBox=\"0 -3 24 24\"></svg> checkbox","children":[],"payload":{"tag":"li","lines":"25,26"}},{"content":"Katex: <math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow><annotation encoding=\"application/x-tex\">x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}</annotation></semantics></math>x=2a−b±b2−4ac<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"1.08em\" viewBox=\"0 0 400000 1080\" preserveAspectRatio=\"xMinYMin slice\"></svg>","children":[{"content":"More Katex Examples","children":[],"payload":{"tag":"li","lines":"27,28"}}],"payload":{"tag":"li","lines":"26,28","fold":1}},{"content":"Now we can wrap very very very very long text with the maxWidth
option","children":[],"payload":{"tag":"li","lines":"28,29"}},{"content":"Ordered list","children":[{"content":"1. item 1","children":[],"payload":{"tag":"li","lines":"30,31","listIndex":1}},{"content":"2. item 2","children":[],"payload":{"tag":"li","lines":"31,33","listIndex":2}}],"payload":{"tag":"li","lines":"29,33"}}],"payload":{"tag":"h3","lines":"21,22"}},{"content":"Blocks","children":[{"content":"console.log('hello, JavaScript')\n
","children":[],"payload":{"tag":"pre","lines":"35,38"}},{"content":"\n\n\nProducts \nPrice \n \n\n\n\nApple \n4 \n \n\nBanana \n2 \n \n\n","children":[],"payload":{"tag":"table","lines":"39,43"}},{"content":"
","children":[],"payload":{"tag":"img","lines":"44,45"}}],"payload":{"tag":"h3","lines":"33,34"}}],"payload":{"tag":"h2","lines":"17,18"}}]},{"colorFreezeLevel":2})</script> </body> </html>


分析以下html 使用这个js 在Java中实现markdown转思维导图生成html:
<html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Markmap</title> <style> * { margin: 0; padding: 0; } html { font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; } #mindmap { display: block; width: 100vw; height: 100vh; } .markmap-dark { background: #27272a; color: white; } </style> </head> <body> <svg id="mindmap"></svg> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser/index.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script> <script>(e=>{window.WebFontConfig={custom:{families:["KaTeX_AMS","KaTeX_Caligraphic:n4,n7","KaTeX_Fraktur:n4,n7","KaTeX_Main:n4,n7,i4,i7","KaTeX_Math:i4,i7","KaTeX_Script","KaTeX_SansSerif:n4,n7,i4","KaTeX_Size1","KaTeX_Size2","KaTeX_Size3","KaTeX_Size4","KaTeX_Typewriter"]},active:()=>{e().refreshHook.call()}}})(()=>window.markmap)</script><script src="https://cdn.jsdelivr.net/npm/[email protected]/webfontloader.js" defer></script><script>(()=>{setTimeout(()=>{const{markmap:S,mm:Q}=window,$=new S.Toolbar;$.attach(Q);const I=$.render();I.setAttribute("style","position:absolute;bottom:20px;right:20px"),document.body.append(I)})})()</script> <script>((l,U,M,R)=>{const N=l();window.mm=N.Markmap.create("svg#mindmap",(U||N.deriveOptions)(R),M),window.matchMedia("(prefers-color-scheme: dark)").matches&&document.documentElement.classList.add("markmap-dark")})(()=>window.markmap,null,{"content":"markmap","children":[{"content":"Links","children":[{"content":"Website","children":[],"payload":{"tag":"li","lines":"8,9"}},{"content":"GitHub","children":[],"payload":{"tag":"li","lines":"9,11"}}],"payload":{"tag":"h2","lines":"6,7"}},{"content":"Related Projects","children":[{"content":"coc-markmap for Neovim","children":[],"payload":{"tag":"li","lines":"13,14"}},{"content":"markmap-vscode for VSCode","children":[],"payload":{"tag":"li","lines":"14,15"}},{"content":"eaf-markmap for Emacs","children":[],"payload":{"tag":"li","lines":"15,17"}}],"payload":{"tag":"h2","lines":"11,12"}},{"content":"Features","children":[{"content":"Lists","children":[{"content":"strong del italic highlight","children":[],"payload":{"tag":"li","lines":"23,24"}},{"content":"inline code
","children":[],"payload":{"tag":"li","lines":"24,25"}},{"content":"<svg width=\"16\" height=\"16\" viewBox=\"0 -3 24 24\"></svg> checkbox","children":[],"payload":{"tag":"li","lines":"25,26"}},{"content":"Katex: <math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow><annotation encoding=\"application/x-tex\">x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}</annotation></semantics></math>x=2a−b±b2−4ac<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"1.08em\" viewBox=\"0 0 400000 1080\" preserveAspectRatio=\"xMinYMin slice\"></svg>","children":[{"content":"More Katex Examples","children":[],"payload":{"tag":"li","lines":"27,28"}}],"payload":{"tag":"li","lines":"26,28","fold":1}},{"content":"Now we can wrap very very very very long text with the maxWidth
option","children":[],"payload":{"tag":"li","lines":"28,29"}},{"content":"Ordered list","children":[{"content":"1. item 1","children":[],"payload":{"tag":"li","lines":"30,31","listIndex":1}},{"content":"2. item 2","children":[],"payload":{"tag":"li","lines":"31,33","listIndex":2}}],"payload":{"tag":"li","lines":"29,33"}}],"payload":{"tag":"h3","lines":"21,22"}},{"content":"Blocks","children":[{"content":"console.log('hello, JavaScript')\n
","children":[],"payload":{"tag":"pre","lines":"35,38"}},{"content":"\n\n\nProducts \nPrice \n \n\n\n\nApple \n4 \n \n\nBanana \n2 \n \n\n","children":[],"payload":{"tag":"table","lines":"39,43"}},{"content":"
","children":[],"payload":{"tag":"img","lines":"44,45"}}],"payload":{"tag":"h3","lines":"33,34"}}],"payload":{"tag":"h2","lines":"17,18"}}]},{"colorFreezeLevel":2})</script> </body> </html>
