-1

I have a method in which I get patientid and scriptInfo in parameters. But if I get them as null, then null is stored in Map and since null is stored so the Map size increases.

I want that if I receive this parameter as null, then I don't want to store them at all since null is itself is no value which is stored in Map and is not of use to me.

private Map<String, List<String>> getValidatedPatientData(ITransaction transaction, String patientId, String scriptInfo) 
  {


      ppvValidatedinfo = new HashMap<String, List<String>>(); // 
      List<String> scriptDetails = new ArrayList<String>();
      scriptDetails.add(scriptInfo);
      ppvValidatedinfo.put(patientId, scriptDetails);
      transaction.setValue(ITransactionHashtableWag.VALIDATED_PPV_PH_NBR, ppvValidatedinfo);

    return ppvValidatedinfo;
  }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614

4 Answers4

1

At the beginning of the method, assuming that the returned map needs to be modified elsewhere:

if (patientId == null || scriptInfo == null)
    return new HashMap<String, List<String>>();

Alternatively, if the returned map won't me modified use an emptyMap():

if (patientId == null || scriptInfo == null)
    return new Collections.emptyMap();

Either way, I'm advocating for the use of the Null Object Pattern.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Lopez Can you please explain that if we get any thing null then why will return a new hashmap. – user1982609 Jan 22 '13 at 16:33
  • Because you would do so. – Fildor Jan 22 '13 at 16:35
  • @user1982609 it's a bad idea to return null values, if it can be avoided at all. Otherwise, you'll have to check on the calling point of this method if the returned value was null. To see what I mean, look at the [Null Object Pattern](https://en.wikipedia.org/wiki/Null_Object_pattern) – Óscar López Jan 22 '13 at 16:36
  • I would have used `return Collections.emptyMap()` (unless the returned map needs to be mutable). – assylias Jan 22 '13 at 16:38
  • @assylias can you please explain in detail I did not get about empty concept – user1982609 Jan 22 '13 at 16:42
  • @assylias yes, that's a valid option too (depending on OP's needs). user1982609 read about it in the [documentation](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#emptyMap%28%29). – Óscar López Jan 22 '13 at 16:46
0

java.util.HashMap supports null as key also value. you should put not null check.

if (patientId != null || scriptInfo != null){...}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

There's a discussion about this in here: Should functions return null or an empty object?.

Returning null is usually the best idea if you intend to indicate that no data is available.

An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned.

So, I think you should just return null and make the proper validation later

if (patientId == null || scriptInfo == null)
    return null;
else {
   ...
}
Community
  • 1
  • 1
Rafael Companhoni
  • 1,780
  • 1
  • 15
  • 31
0

just use ConcurrentHashMap , not only supports your require, also it's thread safe!

Zava
  • 743
  • 6
  • 17
  • huh... Why would thread safety matter when he is creating a **local variable**? That makes little sense. – assylias Jan 22 '13 at 16:53