4

While using Map as a function argument, only values for 3 keys are populated. However when this function is invoked in another function, the user populates values for initial 2 keys and he does not require 3rd key to be assigned with any value. However if 3rd key is not assigned any value then, the 3rd key is display null value.

Is there any way to avoid this. if user does not assign any value to 3rd key, it must be empty instead of null value.

     public String setMapValues(Map<String,String> testMap) throws Exception
  {
    String str="";

    str= testMap.get("a");
    str+=testMap.get("b");
    str+=testMap.get("c");

    info(str);

    return str;
  }



    public void run() throws Exception 
    {
    LinkedHashMap<String,String> myMap = new LinkedHashMap<String,String>();
    myMap.put("a", "James");
    myMap.put("b", "Bond");
    this.setMapValues(myMap);
}

The function calls displays JamesBondnull as the output, instead it should only display JamesBond as the output by ignoring/skipping the null at the end.

Steve
  • 69
  • 1
  • 9

5 Answers5

3

You can use a function like

static String nullAsEmpty(Object o) {
   return o == null ? "" : o.toString();
}

public String setMapValues(Map<String,String> testMap) {
    String str = nullAsEmpty(testMap.get("a")) +
                 nullAsEmpty(testMap.get("b")) +
                 nullAsEmpty(testMap.get("c"));

    info(str);

    return str;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

How about:

String temp = testMap.get("c");
str+= (temp == null : "" : temp);
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

You can implement your version of Map:

import java.util.HashMap;

class MyMap<K, V> extends HashMap<K, V> {  
    @Override
    public V get(Object key) {
        V val = super.get(key);
        if (val != null) {
            return val;
        } else {
            return "";
        }
    }
}

Then just use MyMap instead of Map

vadchen
  • 1,442
  • 1
  • 11
  • 14
1

Or init your map with default values, if you know all keys which could be null

Map getInstance(){

    Map<String,String> myMap = new LinkedHashMap<String,String>();

    myMap.put("a", "");
    myMap.put("b", "");
    myMap.put("b", "");

   return myMap;
}

By putting duplicate keys the old values are replaced by the new ones.

zudduz
  • 2,457
  • 5
  • 24
  • 29
Jan Piel
  • 540
  • 2
  • 6
  • I saw it too late but I must say that Vadchens inheritance solution is better than my one. But there you maybe better inherit it with overriding the Value-Generci parameter directly with String : class MyMap extends HashMap – Jan Piel Apr 11 '13 at 08:18
1

To be complete:

If you use Vadchens answer - which is better - you can do two things:

Extends your map by setting generic value-parameter directly to String

 class MySecMap<K> extends LinkedHashMap<K, String>{
    @Override
    public String get(Object key) {
        String val = super.get(key);
        if (val != null) {
            return val;
        } else {
           return "";
        }
    }
}

Or create a class with an extra interface and a default-value-provider:

interface IDefaultValueProvider<V>{
    V getDefaultValue();
}

class MyMap<K, V, D extends IDefaultValueProvider<V>> extends LinkedHashMap<K, V>{

    private IDefaultValueProvider<V> provider;

    public MyMap(IDefaultValueProvider<V> p){
        super();
        provider = p;
    }

    @Override
    public V get(Object key) {
        V val = super.get(key);
        if (val != null) {
            return val;
        } else {
            return this.provider.getDefaultValue();
        }
    }
}
zudduz
  • 2,457
  • 5
  • 24
  • 29
Jan Piel
  • 540
  • 2
  • 6