Java HashMap重复值-识别重复值
我知道Java HashMap重复值-识别重复值,java,hashmap,duplicates,Java,Hashmap,Duplicates,我知道HashMap不允许插入重复值,它用最新条目替换最后一个重复值。 是否有办法打印在put方法中找到的副本 我有以下代码片段: for( int i = 0; i <= elements.length - 1; i++) { nodeDBList = (NodeList) xPath.compile(elements[i]).evaluate(dbDocument, XPathConstants.NODESET); for (int j = 0; j < node
HashMap
不允许插入重复值,它用最新条目替换最后一个重复值。
是否有办法打印在put
方法中找到的副本
我有以下代码片段:
for( int i = 0; i <= elements.length - 1; i++) {
nodeDBList = (NodeList) xPath.compile(elements[i]).evaluate(dbDocument, XPathConstants.NODESET);
for (int j = 0; j < nodeDBList.getLength(); j++) {
if(nodeDBList.item(j).getFirstChild() != null)
dbList.put(nodeDBList.item(j).getFirstChild().getNodeValue().toLowerCase().trim(),
nodeDBList.item(j).getNodeName().toLowerCase().trim());
}
}
for(int i=0;i错误。HashMap
不支持散列的重复键
对于不同的键,完全可以接受重复值
您可以通过迭代values()
方法并使用equals
方法来搜索现有值
编辑
这里的键和值之间似乎存在混淆
根据HashMap
实现Map
的public V put(K键,V值);
,方法put
将返回给定键的原始值(如果有),或null
引自
@返回与键关联的上一个值,如果存在,则返回null
没有键的映射。(null返回也可以表示映射
以前将null与密钥关联。)
键的旧值由put方法返回,因此您可以输出它
假设HashMap
的值是String
类型:
for( int i = 0; i <= elements.length - 1; i++)
{
nodeDBList = (NodeList) xPath.compile(elements[i]).evaluate(dbDocument, XPathConstants.NODESET);
for (int j = 0; j < nodeDBList.getLength(); j++) {
if(nodeDBList.item(j).getFirstChild() != null) {
String oldVal = dbList.put(nodeDBList.item(j).getFirstChild().getNodeValue().toLowerCase().trim(), nodeDBList.item(j).getNodeName().toLowerCase().trim());
if (oldVal != null) {
System.out.println(oldVal);
}
}
}
}
for(inti=0;i答案可以在中找到:put方法返回先前与键关联的值
返回:
与键关联的上一个值,如果没有键的映射,则为null。(null返回也可以指示映射
以前将null与密钥关联。)
覆盖HashMap
这是一个例子
public class MyMap<K, V> extends HashMap<K,V> {
private static final long serialVersionUID = -1006394139781809796L;
@SuppressWarnings({ "unchecked" })
@Override
public V put(K key, V value) {
if (value == null) {
return super.put(key, value);
}
if (value.getClass() == Timestamp.class) {
DateFormat dateTimeFormatter;
dateTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, getLocale());
super.put((K) (key + "_f"), (V) dateTimeFormatter.format(new Date(((Timestamp) value).getTime())));
DateFormat dateFormatter;
dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, getLocale());
super.put((K) (key + "_f_date"), (V) dateFormatter.format(new Date(((Timestamp) value).getTime())));
}
if (value.getClass() == java.sql.Date.class) {
DateFormat dateFormatter;
dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, getLocale());
super.put((K) (key + "_f"), (V) dateFormatter.format(new Date(((java.sql.Date) value).getTime())));
}
return super.put(key, value);
}
}
公共类MyMap扩展了HashMap{
私有静态最终长serialVersionUID=-1006394139781809796L;
@SuppressWarnings({“unchecked”})
@凌驾
公共V输入(K键,V值){
如果(值==null){
返回super.put(键、值);
}
if(value.getClass()==Timestamp.class){
DateFormat dateTimeFormatter;
dateTimeFormatter=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM,getLocale());
super.put((K)(key+“_f”),(V)dateTimeFormatter.format(新日期((时间戳)值).getTime());
日期格式化程序;
dateFormatter=DateFormat.getDateInstance(DateFormat.SHORT,getLocale());
super.put((K)(key+“_f_date”),(V)dateFormatter.format(newdate((Timestamp)value.getTime());
}
if(value.getClass()==java.sql.Date.class){
日期格式化程序;
dateFormatter=DateFormat.getDateInstance(DateFormat.SHORT,getLocale());
super.put((K)(key+“_f”),(V)dateFormatter.format(newdate((java.sql.Date)value.getTime());
}
返回super.put(键、值);
}
}
我相信,当我们尝试插入重复值时,hashmap会在内部检查该值是否重复并允许使用最新的值。我需要打印在.put方法中遇到的所有重复值。是否有方法打印它们?我实际上有一个键(即xml节点)和多个与键关联的值。我可以看到任何重复的值当前都被忽略,并且最后一个(来自重复值)被添加到映射中。按值,您的意思是键?覆盖HashMap谢谢,上面的代码片段符合我的目的。非常感谢。