Saturday, January 11, 2020

LinkedList(Collections)

LinkedList

Java Collections framework provides the LinkedList class. It is the implementation of the Linked List in the Collections framework. It implements List interface and allows all optional list operations, and permits all elements (including null ). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.

LinkedList Implementation in Java
LinkedList Implementation in Java

  • LinkedList can be used as a stack, queue, or double-ended queue (Deque).
  • LinkedList is not synchronized.
  • Duplicate entries are possible.
  • Insertion and deletion operations are more efficient than ArrayLists.








Example:
LinkedList Example

/***********LinkedList*****************/
import java.util.Iterator;
import java.util.LinkedList;

public class TestLinkedList {

      public static void main(String[] args) {
            LinkedList<String> colors=new LinkedList<>();
            //Generics are type parameters which are provided between < >
            //JVM can now identify the type of collection
            colors.add("RED");
            colors.add("BLUE");
            colors.add("GREEN");
            colors.add("ORANGE");
            colors.add("PURPLE");
            /*for(String s:colors)
                  System.out.println(s);
            */
            Iterator<String> i=colors.iterator();
            while(i.hasNext())
            {
                  System.out.println(i.next());
            }
           
      }
      }
Output:
RED
BLUE
GREEN
ORANGE
PURPLE


Adding Objects of Employee class to a list

/***********LinkedList*****************/
import java.util.LinkedList;

class Employee implements Comparable<Employee>
{
      int empid;
      String ename;
      String dept;
      public Employee(int empid, String ename, String dept) {
            super();
            this.empid = empid;
            this.ename = ename;
            this.dept = dept;
      }
      @Override
      public int compareTo(Employee emp) {//For Sorting by id
            return this.empid-emp.empid;
      }
     
}
public class LinkedListDemo {

      public static void main(String[] args) {
            LinkedList<Employee> emp_list=new LinkedList<>();
            emp_list.add(new Employee(12, "Alex", "Sales"));
            emp_list.add(new Employee(52, "John", "HR"));
            emp_list.add(new Employee(9, "Sam", "Marketing"));
            emp_list.add(new Employee(43, "Dennis", "Sales"));
            emp_list.add(new Employee(95, "Tom", "HR"));
           
            for(Employee e:emp_list)
            {
                  System.out.println(e.empid+"\t"+e.ename+"\t"+e.dept);
            }
      }

}Output:
Output:
12    Alex  Sales
52    John  HR
9     Sam   Marketing
43    Dennis      Sales

95    Tom   HR


Methods on LinkedList

Several methods to operate on the LinkedList in Java are available. Few of them are listed.

add()

To add a new Object in the List.

addAll()

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

addFirst()

Adds an Object at the first position of the List.

addLast()

Adds an Object at the last position of the List.

clear()

Clears all the list

clone()

Return a cloned LinkedList Object

contains()

Return a boolean value on the basis of if the collection contains the input Object or not.

containsAll()

Return a boolean value on the basis of if the collection contains the input Collection or not.

element()

Returns the first element of the LinkedList

get(index)

Returns the Object at the input index.

isEmpty()

Return the boolean value if the list is empty or not.

iterator()

Returns an iterator over the elements in this list in the proper sequence.

getFirst()

Returns the first element of the List.

getLast()

Returns the last Object of the List.

indexOf()

Returns the index of the input object.

remove(Object)

Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

remove(index)

removes the element from the input index.

remove()

removes the first element.

push()

Appends the object at the end of the List.

pop()

Removes the object from the last of the List 

Video Tutorial