File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes List  Vs  LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List  Vs  LinkedList" Watch "List  Vs  LinkedList" New topic
Author

List Vs LinkedList

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
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

Joined: Dec 10, 2008
Posts: 24
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

    Joined: Feb 22, 2007
    Posts: 58
    Dear ShaikSalam Bashoeb, Campbell Ritchie

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

    Regards

    Suresh.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    You're welcome
     
     
    subject: List Vs LinkedList