2014-11-25 62 views
0

據我所知,HashMap不允許插入重複值,並用最新的條目替換上次重複的值。 有沒有辦法打印在put方法中找到的重複項?HashMap重複值 - 確定重複項

我有下面的代碼片段:

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()); 

    } 
} 
+0

的價值觀,你的意思是鑰匙? – codeMan 2014-11-25 13:43:26

+0

重寫HashMap – 2014-11-25 13:45:56

回答

1

密鑰的舊值由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); 
      } 
     } 
    } 
} 
+0

謝謝,上面的代碼片段服務於我的目的。感謝百萬Eran。 – user2967948 2014-11-25 13:54:06

4

錯誤。 HashMap不支持散列的重複鍵。

對於不同的鍵,重複值是完全可以接受的。

您可以通過迭代values()方法並使用equals方法來搜索現有值。

編輯

似乎在這裏鍵和值之間的混淆。

按照HashMap實施Mappublic V put(K key, V value);的,該方法put將返回原來的值給定鍵(如有)或null。從API

報價@return是否有 鍵的映射關係與鍵,或零相關聯的先前的值。 (返回null還可以表示該映射以前 null與key關聯。)

+0

我相信,當我們嘗試插入重複值時,hashmap會在內部檢查是否重複並允許最新值。我需要打印所有在.put方法中遇到的重複值。有沒有辦法打印它們? – user2967948 2014-11-25 13:43:22

+0

@ user2967948你指的是鑰匙。看我的編輯。 – Mena 2014-11-25 13:44:12

+0

我實際上有一個鍵(這是xml節點)和與鍵關聯的多個值。我可以看到任何重複的值目前都被忽略,最後一個值(來自重複值)被添加到地圖中。 – user2967948 2014-11-25 13:53:31

3

那麼,答案可以在the API description of HashMap發現:put方法返回以前與鍵關聯的值。

返回:與鍵關聯 以前的值,或空,如果有鍵的映射關係。 (返回null還可以表示該映射以前 null與key關聯。)

1

改寫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); 
    } 
}