0

I'm trying to initialize a ConcurrentHashMap of ConcurrentHashMaps with

private final ConcurrentHashMap<
    String, 
    ConcurrentHashMap<String, Double>
> myMulitiConcurrentHashMap = new ConcurrentHashMap<
    String, 
    new ConcurrentHashMap<String, Double>()
>();

but javac gives

HashMapper.java:132: error: illegal start of type
    new ConcurrentHashMap<String, Double>()
    ^
HashMapper.java:132: error: '(' or '[' expected
    new ConcurrentHashMap<String, Double>()
        ^
HashMapper.java:132: error: ';' expected
    new ConcurrentHashMap<String, Double>()

pointing to the second new.

How can myMulitiConcurrentHashMap be newly initialized properly?

  • 1
    `new ConcurrentHashMap()` (and `new ..` is an expression) – user2864740 Dec 01 '13 at 23:41
  • Note that it is very difficult to use such a map thread-safely. – SLaks Dec 01 '13 at 23:42
  • 1
    @SLaks Thank-you! Would you mind pointing to a link that outlines the thread-safety problems? Thank-you so much in advance! –  Dec 01 '13 at 23:43
  • Remove the second "new". It doesn't belong there. – Hot Licks Dec 02 '13 at 00:13
  • 1
    @Gracchus: Because you can't make operations atomic. What if one thread modifies an inner list as a different thread replaces it? See also http://blog.slaks.net/2013-07-22/thread-safe-data-structures/ – SLaks Dec 02 '13 at 01:12
  • @SLaks Thank-you, and great blog entry! In my case particular case at this moment, strict atomicity isn't required, but I'm sure I and others will benefit in the future from your very insightful blog entry! I will surely keep it in my back pocket for when I need strict atomicity! –  Dec 02 '13 at 01:15

3 Answers3

5

You do not initialize the inner ConcurrentHashMap<String, Double>; just the following should work:

new ConcurrentHashMap<
    String, 
    ConcurrentHashMap<String, Double>
>();
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
  • Now both of you have used this indentation pattern. I've never seen it before, but there is some logic to it. – Thilo Dec 01 '13 at 23:42
  • @Thilo I personally do not use this indentation pattern, but I chose to keep it consistent with the OP's original code. – Kevin Ji Dec 01 '13 at 23:44
  • @Thilo Right out of the jQuery handbook! ;)) –  Dec 01 '13 at 23:45
3

Generic type parameters are exactly that – types.
It doesn't make sense to have a Map<String, new SomeType()>.
You need to simply write the type of the second parameter.

To paraphrase, you're creating a single new ConcurrentHashMap<K, V>(), which can hold multiple inner maps later.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

By the way, Java 7 has a more concise syntax now (the "diamond"):

private final 
   ConcurrentHashMap<String, ConcurrentHashMap<String, Double>>
      myMulitiConcurrentHashMap =
         new ConcurrentHashMap<>();

You should be able to use interfaces on the left hand side, too:

private final 
   ConcurrentMap<String, ConcurrentMap<String, Double>>
      myMulitiConcurrentHashMap =
         new ConcurrentHashMap<>();
Thilo
  • 257,207
  • 101
  • 511
  • 656