7

I am getting this warning on Sonar:

Avoid using implementation types like 'HashMap'; use the interface instead

What does it mean?

The class in which i get this warning is as:

class A {
   private HashMap<String, String> map=new HashMap<String, String>();

   //getters and setters

}

Please, I want proper solution to avoid warning on Sonar.

EJK
  • 12,332
  • 3
  • 38
  • 55
Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89

3 Answers3

21

You should always code to an interface. ie. in this case you should declare your field like this:

private Map<String, String> map= new HashMap<String, String>();

This way anything using the map variable will treat it as type Map rather than HashMap.

This allows you to swap out the underlying implementation of your map at a later date without having to change any code. You are no longer tied to HashMap

Read through this question: What does it mean to "program to an interface"?

Also I am not sure what you were doing casting to a Set there?

Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
  • 1
    I'd agree it's a generally good practice, but I take issue with the "always" in your first sentence. It's perfectly likely that you might end up needing specialized methods of a `LinkedList` or `LinkedHashSet` or `LinkedHashMap`, for instance, in which case you won't be able to resort to the interface. At least not in Java where there's no compostive datatype declaration syntax and you aren't able to say something like "I want something that's the intersection of the `Map` and `List` interfaces". If you end up passing around the interface, then you lose context. – haylem Jul 08 '14 at 07:41
  • 1
    Beware also that always using the base interface might hide behavioral implementation details. For instance, if you use an immutable `List`, maybe resorting to the `List` interface instead of directly an `ImmutableList` (or another type) is not the best idea, as you convey the wrong impression that your object satisfies the `List` interface, which it doesn't. So, there definitely are exceptions to this rule. – haylem Jul 08 '14 at 07:42
  • Thats true regarding the use of specialized methods, though these cases are very rare so I wouldnt worry too much abouyt the wording :). I think with regard to your second point I would still use the interface as it leaves you with the option to reuse code if you decide to use other list implementations later. You just need to use correct exception handling. – cowls Jul 08 '14 at 07:50
  • @cowis How can I specify Map which is Serializable? When I use Map my implementation breaks. Is there a way I can achieve this? – javaDeveloper Nov 17 '21 at 17:30
5

I dont use Sonar, but what the warning basically means is

Always program to an interface rather than implemnetation class

private Map<String, String> map= new HashMap<String, String>();
        Interface                    Implementing class
PermGenError
  • 45,977
  • 8
  • 87
  • 106
4

Generally, you should always implement against an interface instead of a concrete type. In this example it means you should write your code like that:

private Map<String, String> map= new HashMap<String, String>();

The big advantage is that you can then later always change the concrete implementation of your Map without breaking the code.

To get more details about it, check out this question: What do programmers mean when they say, "Code against an interface, not an object."?

Community
  • 1
  • 1
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • I always code against interfaces, but you are right using the word "generally". For instance, GWT somehow prefers using directly the implementation, because so the compiling performance are increased. – ThanksForAllTheFish Jan 18 '13 at 10:15