1

Which of the following is correct:

Map<K,V> map = new HashMap<>();

or

HashMap<K,V> map = new HashMap<>();

or even:

List<T> list = new ArrayList<>();

ArrayList<T> list = new ArrayList();

I am curious if there is a correct way to this or is it just a matter of preference. This could be silly of me to be wondering, but I just wasn't sure.

August
  • 12,410
  • 3
  • 35
  • 51
Philector
  • 91
  • 8

2 Answers2

1

If at all possible (so unless you're writing some utility code for a specific implementation of a collection or something), you want to use the interface, I.e.

List<String> list = new ArrayList<>();

That way, if the need arises down the road, it's easier to switch implementations, such as moving to a LinkedList.

Dogs
  • 2,883
  • 1
  • 19
  • 15
1

An advantage of doing something like

Map map = new HashMap<>();

is that your code is not dependent on the implementation of HashMap nor its API.

If you later decide to substitute the HashMap for another implementation of the Map interface such as EnumMap then the only part of your code that changes is that line.

Map map = new EnumMap<>();

Had you instead done something like

HashMap map = new HashMap<>();

and then decided to change from HashMap to EnumMap, you'd have to:

1) Go all over your code and change every instance of HashMap to EnumMap.

2) Redesign your code entirely if you depended on functionality that is present in HashMap and not EnumMap. That is, if you depended on an API that Map does not support then you cannot necessarily and easily change from one type to another without doing additional modifications to your code.

iFytil
  • 459
  • 3
  • 7
  • There you're using the raw type, is that also advantageous? My prof advised against it.. – Philector Feb 22 '15 at 04:14
  • Your professor might not be considering the cost of maintaining software. It's not only about having correct and efficient code: code needs to be easily refactorable, readable, as well as many other aspects which make it easy to deal with. Did your professor tell you why she was against this practice which is, as far as I know, commonly accepted? – iFytil Feb 22 '15 at 04:16
  • He didn't say anything to back it up, but from what I assumed was the ability to catch errors at compile time rather than run time. – Philector Feb 22 '15 at 04:21
  • There might have been a misunderstanding between you and your professor. Doing the method I advise for cannot result in any runtime bugs that would have been avoided by not following it. This is because the runtime type of variable is going to be HashMap whether you assign it to a Map or a HashMap. The code it will execute, and therefore its behavior, is independent or the practice used. – iFytil Feb 22 '15 at 04:24
  • I encourage you to discuss this with your professor in more detail and, perhaps, come back to us with some additional context :) – iFytil Feb 22 '15 at 04:25
  • Like I said, I am learning...and I have a LONG way to go. Thanks for your help! I really appreciate it. – Philector Feb 22 '15 at 04:27
  • That sounds like the perfect advice...thats what he's there for, right? lol – Philector Feb 22 '15 at 04:27