Java 使用HashMap比较字符串
我很少有这样的xml文件Java 使用HashMap比较字符串,java,xml,Java,Xml,我很少有这样的xml文件 <?xml version = "1.0"?> <note> <to>Tim</to> <from>Joe</from> <head>About Job</head> </note> <?xml version = "1.0"?> <note> <to>Tim</to> <from>Joe</fr
<?xml version = "1.0"?>
<note>
<to>Tim</to>
<from>Joe</from>
<head>About Job</head>
</note>
<?xml version = "1.0"?>
<note>
<to>Tim</to>
<from>Joe</from>
<head>How are u?</head>
</note>
<?xml version = "1.0"?>
<note>
<to>Marry</to>
<from>Pit</from>
<head>Welcome to home</head>
</note>
FROM:
Tim
Tim
Pit
TO:
Joe
Joe
Marry
HEAD:
About Job
How are u?
Welcome to home
我希望名称不要重复
我如何使用hasMap,请帮助我!:) 如果您只需要一个唯一的名称集合,请使用
HashSet
。每个唯一名称只存储一个实例<如果您希望每个唯一的名称都作为键,那么code>HashMap是有意义的。在这种情况下,您必须决定要存储哪些内容作为每个名称键的值。从您的问题来看,您似乎只需要存储一组唯一的对象。Set
的实现将有所帮助。您可以使用String
对象的HashSet
:
Set uniqueNames=newhashset()
Set from=newhashset();
您只需使用add方法添加新记录。请记住,在已插入的巫术元素中,集合不会保持秩序。但我想在您的示例中,您将在打印之前对名称进行排序,这样就可以了
如果您想保持迭代顺序,请使用LinkedHashSet。读取文本文件并将其存储到类似的
哈希集中
Set names = new HashSet<String>();
names.put(nameString);
希望这有帮助
Set names = new HashSet<String>();
names.put(nameString);
names.put(nameString.toLowerCase()); \\now Tim and tim will be treated equally