My question today is not a problem-solving question, but a best-practice theory question. I am getting familiarized with Joda-Time, and I read this in the user guide (http://joda-time.sourceforge.net/userguide.html#Interface_usage):
When working with a Collections interface, such as List or Map you will normally hold your variable as a type of List or Map, only referencing the concrete class when you create the object.
List list = new ArrayList(); Map map = new HashMap();
This struck me as quite an odd statement. In my programming, I've always held the concrete class for my variables unless I'm programming an API of some sort. Does anyone have any helpful material to explain the reasons why it would be preferential to hold the general/abstract class vs the concrete class for your variable type?
To illustrate, I wrote a simple method that I use whenever I need a list of objects as a string separated by commas:
public static <T> String CSV(Collection<T> list) {
boolean first = true;
StringBuilder sb = new StringBuilder();
for(Object e : list) {
if(first)
first = false;
else
sb.append(", ");
sb.append(e.toString());
}
return sb.toString();
}
Here, of course, you must use Collection so you can pass in anything you want, Object[], LinkedList, etc.
But let's say elsewhere I'm storing a set of strings and I want it to be a LinkedHashSet, I would create the variable like this:
LinkedHashSet<String> set = new LinkedHashSet<String>();
or
LinkedHashSet<String> set = new LinkedHashSet<>();
(because of the changes in Java 7)
but according to Joda-Time's user guide, I should do this:
Set<String> set = new LinkedHashSet<String>();
?
I just don't see the advantages. Anyone have helpful input/reading material?
Thanks!