我有两个单独的地图,其中项目列表作为值。我正在从两个单独的 xml 文件中读取数据以填充这些地图。地图的内容如下所示:
Map<String,List<String>> hMap1 = new HashMap<>();
Map<String,List<String>> hMap2 = new HashMap<>();
hmAP1 key:Bob val[aa,bb,cc,dd,ee]
key:Sam val[ss,tt,uu,vv,ww]
hMap2 key:Dan val[xx,pp,yy,qq,zz]
key:Bob val[cc,dd,hh,kk,mm]
我想比较 hMap1 和 hMap2 中的值。在这种情况下,hMap1 [cc, dd] 中的 Bob 具有与 hMap2 [cc, dd] 中的 Bob 相似的值。如何仅将 Bob 和匹配值添加到新的 hMap3。拜托,我似乎无法理解。以下是我在阅读 xml 文件并添加到 hashMaps 方面所做的工作:
public static Map<String,List<String>> checkSimilarValues (File file) throws TransformerException, ParserConfigurationException, SAXException, IOException
{
Map<String, List<String>> hMap = new HashMap<>();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc1 = dBuilder.parse(file);
// System.out.println(file.getName());
doc1.getDocumentElement().normalize();
NodeList nList = doc1.getElementsByTagName("class");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
// list of include methods
NodeList includeMethods = eElement.getElementsByTagName("include");
for (int count = 0; count < includeMethods.getLength(); count++) {
Node node1 = includeMethods.item(count);
if (node1.getNodeType() == node1.ELEMENT_NODE) {
Element methods = (Element) node1;
List<String> current =
hMap.get(eElement.getAttribute("name"));
// List<String> current2 =
map.get(eElement.getAttribute("name"));
if (current == null) {
current = new ArrayList<String>();
hMap.put(eElement.getAttribute("name"), current);
}
if (!(current.contains(methods.getAttribute("name")))) {
current.add(methods.getAttribute("name"));
}
}
}
}
}
return hMap;
}
public static void main (String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException
{
File f1 = new File("sample1.xml");
File f2 = new File("sample2.xml");
Map<String, List<String>> hMap1 = new HashMap<>();
Map<String, List<String>> hMap2 = new HashMap<>();
hMap1 = checkSimilarValues(f1);
hMap2 = checkSimilarValues(f2);
for (String key : hMap1.keySet()) {
System.out.println(key);
for (String string : hMap1.get(key)) {
System.out.println(string);
}
}
}
示例1.xml
<classes>
<class name="Bob">
<methods>
<include name="cc" />
<include name="cc" />
<include name="hh" />
<include name="kk" />
<include name="mm" />
</methods>
</class>
<class name="Dan">
<methods>
<include name="xx" />
<include name="pp" />
<include name="yy" />
<include name="qq" />
<include name="zz" />
</methods>
</class>
示例2.xml
<classes>
<class name="Bob">
<methods>
<include name="aa" />
<include name="bb" />
<include name="cc" />
<include name="dd" />
<include name="ee" />
</methods>
</class>
<class name="Sam">
<methods>
<include name="ss" />
<include name="tt" />
<include name="uu" />
<include name="vv" />
<include name="ww" />
</methods>
</class>