0

I know it's maybe not the best thing to do and that I'm not using properly this object, but here is my problem. I want to use a ConcurrentSkipListMap and have declared my map as such:

private ConcurrentSkipListMap<Date,T> map 

Sometimes, I need to initialize elements of this map with a "null" value, meaning that on a given day, there were no data. For a regular hastable, that works, you can insert a null value associated to a key. When getting the value for a certain date, I just have to test if it is null or not. For a ConcurrentSkipListMap, that does not work. It throws a null value exception. When looking at the source code, I understood that this is an internal representation of the Nodes where a null value just means that the Node in the list is dead.

I know the best answer to this would be, just don't add the date in that case... but for many reasons, I still need to. And anyway, testing for the existence of the key is to me the same as testing for a null value associated to a key. I can't add a dummy value as well cause I will not be able to make a distinction between this dummy value and a real one (for, example, if my template class is an Integer, even if I choose -1 as a flag for empty, i would not be able to see the difference with a real value of -1 for this day).

I was previously using

private SortedMap<Date,T> map = Collections.synchronizedSortedMap(new TreeMap<Date,T>(new BasicDateComparatorIncreasing()));

but I like the fact that ConcurentSkipListMap implements the ConcurrentNavigableMap interface

Is there a way to achieve what I want to do ?

thanks

BlackLabrador
  • 2,829
  • 5
  • 18
  • 21

1 Answers1

2

How about

private ConcurrentSkipListMap<Optional<Date>, T> map;

?

Then to add elements:

map.put(Optional.fromNullable(date), value);

And when accessing key check Optional.isPresent() and call Optional.get().

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html

pingw33n
  • 12,292
  • 2
  • 37
  • 38
  • Hi, Thanks for your help, I need to investigate this. At first glance, my problem is related to the value, not the key. My keys are never null. Only the value could be. Sorry if my previous question was not clear on this point – BlackLabrador Jun 13 '14 at 09:03
  • @BlackLabrador you can use the same approach for the value: `ConcurrentSkipListMap>`. – pingw33n Jun 13 '14 at 09:07
  • Works perfectly well !!!! Thanks for the answer @pingw33n. A quick one, I'm going to encapsulate this in a new class so that I don't have to change the behavior of the remaining code. At one point, I have function that returns an array list of all the values `code` ArrayList array = new ArrayList(map.values());`code` – BlackLabrador Jun 15 '14 at 10:16
  • A quick one, I'm going to encapsulate this in a new class so that I don't have to change the behavior of the remaining code. At one point, I have function that returns an array list of all the values `ArrayList array = new ArrayList(map.values());`. Is there an efficient way to return the original values and not an `Optional` class in the array list (without going through a second iteration of the list to test `isPresent`) – BlackLabrador Jun 15 '14 at 10:23
  • Apparently, Java 8 has now the Optional Class as well. – BlackLabrador Jun 24 '14 at 15:22