Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List Vs LinkedList

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

When I declare LinkedList like:

List<String> l = new LinkedList<String>()
it does not support any of the methods from the interface Queue even though LinkedList implements Queue interface. (ex: l.poll(), l.peek())

But when I declare like:

LinkedList<String> l = new LinkedList<String>()
then it supports these methods.

Could anyone please explain me this,

thanks in advance.

Suresh.
 
ShaikSalam Bashoeb
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we compile the code. Compiler doesn't let you do the compile sucessfully because of this code



what happens exactly is , when we call poll method ,At compile time, compiler only checks the reference variable "l" type i.e., "List" contains poll method or not. if List contains that method then only compiler let you compile the code sucessfully....

I hope you understood...........

[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]

[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]
[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ShaikSalem is correct, but maybe a little more detail would be welcome.

Go through the Java Tutorials, and look particularly at the List and Queue interfaces. If you look at the API you find that LinkedList is a kind of List, and it is also a kind of Queue (look at the top where it says about interfaces).
If you say List<String> lll = new LinkedList<String>(); the compiler interprets that as:
  • Suresh wants a List.
  • At the moment he wants a LinkedList, but he might want to change to an ArrayList later. [You will never find a poll method in ArrayList.]
  • He said "poll" but I can't find poll in "List".
  • I had better not compile this bit.
  • If you click on the names of the interfaces (or find the full descriptions of the methods) in the API link, you can work out which interfaces the methods come from. If you said "List" then you are going to use the LinkedList as a List; if you said Queue then it would mean you are going to use the LinkedList as a Queue. In both instances you get the methods of those interfaces, and the other methods are not visible to the compiler.

    If you had said Queue<String> qqq = new LinkedList<String>(); the compiler would have only found the methods which are in the Queue interface; you could then change the LinkedList to an ArrayDeque and it would still work.
     
    Suresh Rajadurai
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dear ShaikSalam Bashoeb, Campbell Ritchie

    Thank you so much for your beautiful explanations. I really appreciate it.

    Regards

    Suresh.
     
    Campbell Ritchie
    Sheriff
    Posts: 48917
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic