0

I am trying to replace my use of ConcurrentHashMap with mine CaseInsensitiveConcurrentHashMap. However, I cannot instantiate my hashmap. I will provide my implementations.

The hash map class - based from SO question: https://stackoverflow.com/a/8237007/850475

import java.util.concurrent.ConcurrentHashMap;

public class CaseInsensitiveConcurrentHashMap <T> extends ConcurrentHashMap<String, T>{

    @Override
    public T put(String key, T value) {
        return super.put(key.toLowerCase(), value);
    }

    public T get(String key) {
        return super.get(key.toLowerCase());
    }
}

And my code below that does not work with the new hash map:

public class tester {
    private static ConcurrentMap<String, SomeClass> items = new CaseInsensitiveConcurrentHashMap<String, SomeClass>();  
    private static ConcurrentMap<String, CaseInsensitiveConcurrentHashMap<String, Collection<SomeClass2>>> tagMap = new CaseInsensitiveConcurrentHashMap<String, CaseInsensitiveConcurrentHashMap<String, Collection<SomeClass2>>>();
}

Both lines in tester fail. This is my error message:

Incorrect number of arguments for type CaseInsensitiveConcurrentHashMap<T>; it cannot be parameterized with arguments <String, Collection<SomeClass>>

Any idea as of what I can try?

Community
  • 1
  • 1
Stephen D
  • 2,836
  • 4
  • 27
  • 40
  • Your map has only one type parameter, so you should pass it only one (remove the `String`s) – kiheru Aug 19 '13 at 15:34
  • 1
    Be aware, that your get method is not going to get called since it is not overriding Map.get(). You should also override the other put methods in ConcurrentHashMap. – Michael Krussel Aug 19 '13 at 15:38

2 Answers2

3
class CaseInsensitiveConcurrentHashMap <T>

Your hashmap takes a single generic parameter.

Therefore, when you use it, you must pass only one generic parameter.

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

The CaseInsensitiveConcurrentHashMap has only one generalized parameter for the values, so it should be instanced using new CaseInsensitiveConcurrentHashMap<SomeClass>()

cyberz
  • 884
  • 1
  • 8
  • 16
  • Will this index the generic class by `String`? – Stephen D Aug 19 '13 at 15:41
  • @StephenD By extending `ConcurrentHashMap`, `CaseInsensitiveConcurrentHashMap` has already provided `String` as the type argument for keys. – Paul Bellora Aug 19 '13 at 15:43
  • 1
    Yes it should using case insensitive keys. Also note that a much better approach would be using composition over inheritance since you can't be sure that all methods will self-use the overridden put() (eg. putAll). – cyberz Aug 19 '13 at 15:43
  • Is there a way to allow initializing a new instance like `ConcurrentMap > cichm = new CaseInsensitiveConcurrentHashMap>();` ? – Stephen D Aug 19 '13 at 15:47
  • Your solution works for me. +1 if I can find a way to use it the way in my last comment question – Stephen D Aug 19 '13 at 15:52
  • 2
    It would be enough to parametrize the generic class with 2 parameters, anyway since the implementation relies on the fact that the keys are strings there's no point in doing that. – cyberz Aug 19 '13 at 15:53