Difference between different implementations assigned to a wider type in Java

I know its best practice is to assign subclass implementations to interface type variables to provide this flexibility:

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

      

My real understanding is that when you declare list

as a type, list

you limit its functionality to only the implementation of methods that type list

, and do not allow implementation-specific methods. However, what's the difference between:

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

      

and

List<Integer> list = new LinkedList<Integer>();

      

Apart from some obvious performance differences caused by different implementations of each class for the List interface, are there any differences?

As another example using Set

, I know that doing:

Set<String> set = new HashSet<String>();

      

gives HashSet

how Set

, whereas:

Set<String> set = new TreeSet<String>();

      

gives TreeSet

as Set

, which means (among other things) that it is Set

automatically sorted. But not automatic sorting of an implementation-specific function of a class?

+3


source to share





All Articles