0

I understand that HashMap doesn't allow insertion of duplicate values and it replaces the last duplicate value with the latest entry. Is there a way to print the duplicates which were found during the put method?

I have the following code snippet:

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

    }
} 
2
  • 1
    by values, you mean keys? Commented Nov 25, 2014 at 13:43
  • override the HashMap Commented Nov 25, 2014 at 13:45

4 Answers 4

5

Wrong. HashMap does not support duplicate keys, which are hashed.

Duplicate values are totally acceptable for different keys.

You can search for existing values by iterating them through the values() method and using the equals method.

Edit

There seems to be a confusion between keys and values here.

According to the HashMap implementation of Map's public V put(K key, V value);, the method put will return the original value for a given key if any, or null.

Quote from the API

@return the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

Sign up to request clarification or add additional context in comments.

2 Comments

I believe, when we try to insert a duplicate value, hashmap internally checks if thats a duplicate and allows the latest value. I need to print all those duplicate values encountered during the .put method. Is there a way to print them?
I actually have a key (which is the xml node) and multiple values associated with the key. I can see that any duplicate values are currently ignored and the last one(from the duplicate values) gets added to the map.
3

Well, the answer can be found in the API description of HashMap: The put method returns the value that was previously associated with the key.

Returns: the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

Comments

1

The old value of the key is returned by the put method, so you can output it.

Assuming the value of your HashMap is of type 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);
            }
        }
    }
} 

1 Comment

Thanks, the above code snippet serves my purpose. Thanks a million Eran.
1

Override the HashMap

this is an example

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.