user2173142
user2173142

Reputation: 39

Differentiate programmatically between LinkedList and List

I want to know difference between List and LinkedList programmatically. Can any one help me?

List list=new LinkedList();

Upvotes: 1

Views: 429

Answers (4)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

  • List: The List extends the Collection interface. A List is a collection with an ordered sequence of elements and may contain duplicates. ArrayList, LinkedList and Vector are implementations of a List interface. (i.e. an index based)

  • LinkedList doesn't allocate memory to the items before the items are added to the list. Each item in a LinkedList holds a pointer to the next in the list.LinkedList is implementation of a List interface.

enter image description here

enter image description here

Upvotes: 1

taufique
taufique

Reputation: 2751

LinkedList is a particular type of data structure, where List is implemented using LinkedList. You can create a List Interface implementing anyway you like even using array (though usually implemented using LinkedList). Main thing is you just provide some functionality with the interface. But LinkedList itself is universal in nature.

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

List is an interface, LinkedList is a Concrete Class which implements the List interface. So, you can create an object of LinkedList (concrete class) but not List (interface). Every LinkedList is also a List, so you can do

List list=new LinkedList();

Linked List declaration :

public class java.util.LinkedList extends java.util.AbstractSequentialList implements java.util.List, java.util.Deque, java.lang.Cloneable, java.io.Serializable {
}

Upvotes: 0

stinepike
stinepike

Reputation: 54672

List is the interface and LinkedList is a subclass of List which implements the methods (plus some other utility methods) of List interface.

And HashMap is a totally different type of datastructure implementing Map which stores key value pairs.

So don't be confused about List, LinkedList, HashMap ... better ready the structures of these.

Upvotes: 1

Related Questions