2

I am confused about the LinkedList. They do not really make sense to me. Could someone explain to me how it basically works. For example I saw this code (below) and it did not make sense to me. From what I'm guessing a string list (list1) is created and is put in a LinkedList. But what is diffrence between list and LinkedList ?? Thanks

List<String>list1 = new LinkedList<String>() 

5 Answers5

2

LinkedList is a class implementing the List interface. A interface is not a actual class, its more like a convention/blueprint of what methods that classes implementing them needs to expose.

For example, if you have interface called Shape

interface Shape() {
     public void draw(); 
}

notice that this contain a method body for draw, rather, its a convention saying all classes which implents this interface must have a method called draw. Now, lets say you have two classes which both implements the above interface

class Box implements Shape {
    public void draw() {
        //do actual drawing in this method body
    }
}

class Circle implements Shape {
    public void draw() {
        //do actual drawing in this method body
    }
}

You can then cast instances of either Circle or Box into a Shape variable, and safely call the draw method.

JustDanyul
  • 13,813
  • 7
  • 53
  • 71
2

This line assigns an empty LinkedList object for containing String to a List interface.
The idea of the List interface is, that you later could exchange the LinkedList by an ArrayList whitout the need to change the rest of your code

E.g:

List<String> list1 = new ArrayList<String>();

list.add("element1");

list.add() works for both, for ArrayList and for LinkedList, thats the purpose of the List Interface

AlexWien
  • 28,470
  • 6
  • 53
  • 83
1

java.util.List is an interface and java.util.LinkedList is class which implements List interface.

List<String>list1 = new LinkedList<String>();
SuperClass(Class or interface) ref= new SubClass(Implementing class/concrete class)

The above is polymorphic way to create an LinkedList.

Below are some of the characterstics of List:

  1. Accepts duplicates
  2. Can access using indexes.
  3. Maintains Insertion Order
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

LinkiedList implements the List interface so LinkedList is a List.

This is the definition of the LinkedList Class:

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable

As you can see the LinkedList class implements the List interface.

Therefore, as LinkedList is a List (or a child of List bot not exactly), you can use polymorphism to instantiate a List object like you are saying. The List interface say the behavior of the class that implement this interface, in this case LinkedList. Remember that the Interfaces are like a contract, so A*LL THE METHODS THAT ARE DEFINIED IN* List interface MUST BE IMPLEMENTED in the LinkedList class. In that way List interface has the same methods as the LinkedList class.

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
ChuyAMS
  • 480
  • 2
  • 9
0

I think what is confusing you is the use of generics.

Here it is without generics:

List list1 = new LinkedList();

This is pretty simple Java code. We're creating a LinkedList object and assigning it to a variable of type List. LinkedList implements the List interface so this works.

Now let's add the generics back:

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

All that is different is that you're specifying what the LinkedList is going to contain.

To put something in the list:

list1.add("Something in my linked list");
Sarel Botha
  • 12,419
  • 7
  • 54
  • 59