0

Suppose we wanna define and use a LinkedList in our code. I would define it this way:

LinkedList<String> list= new LinkedList<>(); 

Why some people use the interface List

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

to define a LinkedList? What are the advantages?

C graphics
  • 7,308
  • 19
  • 83
  • 134

1 Answers1

0

If you use following :

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

Then everywhere, in your code, you just using List<String> list. It means, that if you want use ArrayList, the only thing you have to do is to change one line : List<String> list = new ArrayList<>(); and it will work perfectly in all other code, no matter how complex it is.

If you are using LinkedList, you can have huge problems with switching to something else.

libik
  • 22,239
  • 9
  • 44
  • 87