CodingBison

As we know List is an ordered Collection, all the objects in the List can be accessed/manipulated using an integer index, which represents the position at which they are stored in the List. More details about List interface is available here.



Figure: List Interface and Class Hierarchy

One of the two classes that implements the interface List, is a LinkedList. The LinkedList class implements all the methods defined in the List interface and it also provides some extra methods that helps in manipulating the objects stored in the LinkedList.

The linked list offered by the LinkedList class implementation is, Doubly-Linked list. To refresh your memory about doubly-linked list, visit this page Understanding Doubly-Linked list. LinkedList class provides all the standard operations that are associated with a Doubly-Linked list. We will see more about the methods/operations provided by LinkedList class in the next section.

LinkedList Class Constructors

ArrayList class provides two Constructors; we list them in the table below.


Table: Constructors provided by LinkedList Class
ConstructorDescription
LinkedList()Creates an empty LinkedList.
LinkedList(Collection c)Creates a LinkedList with all the elements of the specified Collection "c".

Here is an Example program, that demonstrates the usage of all the above constructors.

 import java.util.LinkedList;
 import java.util.ArrayList;

 public class LinkedListClassDemo {
     public static void main(String[] args) {
         // Creates an Empty LinkedList.
         LinkedList llistObj1 = new LinkedList();

         // Creates an Array list and adds few objects.
         ArrayList alistObj = new ArrayList();
         alistObj.add(new Integer(10));
         alistObj.add(new Integer(23));

         // Creates a linked list from the ArrayList.
         LinkedList llistObj2 = new LinkedList(alistObj);

         System.out.println(" Empty Linked List: " + llistObj1);
         System.out.println(" Non-empty Linked List: " + llistObj2);
         System.out.println(" Non-empty Array List: " + alistObj);
     }
 }

In the above example, llistObj1 is created using the default constructor. So, llistObj1 will be an empty LinkedList. It also creates an ArrayList object "alistObj", which will be used to demonstrate the next LinkedList constructor. The object "llistObj2" is created using the constructor "LinkedList(Collection c)", where alistObj is passed as a parameter. So, this means a LinkedList is being formed using the elements from the Collection object, "alistObj" in this case. All the 3 lists are printed on to the stdio, at the end of the program.

Let us Compile/run the program, Here is the output:

  Empty Linked List: []
  Non-empty Linked List: [10, 23]
  Non-empty Array List: [10, 23]

LinkedList Class Methods

As we already know, LinkedList is one of the Classes that implements the List interface. So, LinkedList class implements all the methods that are defined in the List interface in-addition to its own methods. From what ever we have seen so far, we know that LinkedList class implements List Interface, in fact it also implements some other interfaces like "Queue", "Deque", so LinkedList class also implements the methods defined in "Queue" and "Deque" interfaces. It is recommended to go through both the interfaces ( Queue interface and Deque interface ) for deeper understanding on why/how LinkedList is used as a Queue or Deque.

The below table has the list of the methods that are implemented by the LinkedListClass. The below methods are defined in the List interface, but are discussed here since it makes sense to convert a LinkedList into an ArrayList. All the examples used in the List interface, have used "ArrayList" to demonstrate the usage, so the below methods are not discussed in the List interface section, as it does not make a lot of sense to use the below methods on an ArrayList. You can imagine this table to be an extension of the table List Interface Methods List.


Table: Methods provided by LinkedList Class
MethodDescription
object[] toArray()Returns the array containing all the elements in the list.
object[] toArray(t[] array)Returns the array containing all the elements in the list. But, the returned array type is of the type specified as the argument.

Here is an Example program, that demonstrates the usage of the above mentioned methods.

 import java.util.LinkedList;
 import java.util.ArrayList;

 public class LinkedListClassDemo {
     public static void main(String[] args) {
         LinkedList llistObj1 = new LinkedList();
         llistObj1.add("237");
         llistObj1.add("101");
         llistObj1.add("880");

         System.out.println(" Linked List: " + llistObj1);
         Object[] arrayObj = llistObj1.toArray();

         for (int i = 0; i < arrayObj.length; i++) {
             System.out.println(" Array Obj " + i + ": " + arrayObj[i]);
         }
     }
 }

Let us Compile/run the program, Here is the output:

  Linked List: [237, 101, 880]
  Array Obj 0: 237
  Array Obj 1: 101
  Array Obj 2: 880

Clone Operation on the LinkedList

List interface provides an other important operation called "clone" , where one can get to make a shallow copy of the LinkedList. So, what a shallow copy mean ? It means the elements of the List are not copied, it just just creates an instance of the LinkedList. A change made using one object, gets reflected to the other object. Basically, if you are familiar with "C" programming it like having two pointers point to the same variable, where the pointer points to the address of a variable and changes made to the variable at that address will be seen by all the pointers pointing to that address.

An example program demonstrating the concept of "Clone" method is available in Array List Class. The concept of "Clone" is same, be it an ArrayList or a LinkedList or a aHashSet or a TreeSet.





comments powered by Disqus