user2684215
user2684215

Reputation: 73

java HashMap keys comparison with low throughput

I want to check keys of origMap with otherMap .if it found take the value from othermap as key and value of origMap as value

place it into new hashmap. if not found calculate the all values of origmap using Bigdecimal place in the same map as key "other" and value as bigdecimal output. I am trying like below but it's not working throwing null pointer,not sure what is the issue.

Maps:

HashMap < String, Object > origMap = new HashMap < String, Object > ();
origMap.put("test", "1");
origMap.put("test2", "100.00");
origMap.put("test3", "3");
origMap.put("test4", "300.23");

HashMap < String, Object > otherMap = new HashMap < String, Object > ();
otherMap.put("test3", "fee");
otherMap.put("test2", "tax");

code:

Map newMap = new HashMap();
BigDecimal value1 = null;
for (Map.Entry <? , ?> me: origMap.entrySet())
{
    String key = "";
    String value = "";
    if (otherMap.get(key).equals(me.getKey()))
    {
        key = otherMap.get(me.getKey()).toString();
        value = origMap.get(me.getKey()).toString();
        newMap.put(key, value);
    }
    else
    {
        value = origMap.get(me.getKey()).toString();
        value1 = value1.add(new BigDecimal(value));
    }

    queryMap.put("others", value1);
}

Upvotes: 0

Views: 682

Answers (1)

Thomas
Thomas

Reputation: 88727

otherMap.get(key) will not find an entry for key="" and thus the call to equals(...) will throw a NPE.

Since you seem to try and check whether there is an entry for me.getKey() in otherMap try otherMap.get(me.getKey()) != null or otherMap.containsKey(me.getKey()=) instead.

Additionally, otherMap.get(key).equals(me.getKey()) will never be true in your case (independent on the value of key), since you're comparing the value from otherMap with the key from origMap.

Also please note that calling toString() might result in a NPE as well, unless you are sure that there are no null values.

I'll try and restructure your code to what I think you want:

Map<String, String> newMap=new HashMap<>(); //as of Java 7
BigDecimal value1=null;
for (Map.Entry<String,Object> me : origMap.entrySet()) {  
  if(otherMap.containsKey( me.getKey() )) {
    Object otherValue = otherMap.get(me.getKey());
    Object origValue =  origMap.get(me.getKey());
    String key = otherValue != null ? otherValue.toString() : null; //note: this might cause problems if null keys are not allowed
    String value = origValue != null ? origValue.toString() : null;
    newMap.put(key, value);
  }else {
    Object origValue =  origMap.get(me.getKey());
    if( origValue != null ) {
      value1=value1.add(new BigDecimal( origValue.toString())); //note: this might cause NumberFormatException etc. if the value does not represent a parseable number
    }
  }

  queryMap.put("others", value1);
}

Btw, why are origMap and otherMap of type Map<String, Object> if all values are strings? In that case Map<String, String> would fit better, thus removing the need for the toString() calls (and the null checks as well).

Upvotes: 1

Related Questions