Skip to main content

Linkedlist in Java

LinkedList in Java

LinkedList is an implementation of List interface.

Example of LinkedList in Java

import java.util.*;
public class LinkedListExample {
     public static void main(String args[]) {

         /* Linked List Declaration */
         LinkedList<String> linkedlist = new LinkedList<String>();

         /*add(String Element) is used for adding 
          * the elements to the linked list*/
         linkedlist.add("Item1");
         linkedlist.add("Item5");
         linkedlist.add("Item3");
         linkedlist.add("Item6");
         linkedlist.add("Item2");

         /*Display Linked List Content*/
         System.out.println("Linked List Content: " +linkedlist);

         /*Add First and Last Element*/
         linkedlist.addFirst("First Item");
         linkedlist.addLast("Last Item");
         System.out.println("LinkedList Content after addition: " +linkedlist);

         /*This is how to get and set Values*/
         Object firstvar = linkedlist.get(0);
         System.out.println("First element: " +firstvar);
         linkedlist.set(0, "Changed first item");
         Object firstvar2 = linkedlist.get(0);
         System.out.println("First element after update by set method: " +firstvar2);

         /*Remove first and last element*/
         linkedlist.removeFirst();
         linkedlist.removeLast();
         System.out.println("LinkedList after deletion of first and last element: " +linkedlist);

         /* Add to a Position and remove from a position*/
         linkedlist.add(0, "Newly added item");
         linkedlist.remove(2);
         System.out.println("Final Content: " +linkedlist); 
     }
}
Output:
Linked List Content: [Item1, Item5, Item3, Item6, Item2]
LinkedList Content after addition: [First Item, Item1, Item5, Item3, Item6, Item2, Last Item]
First element: First Item
First element after update by set method: Changed first item
LinkedList after deletion of first and last element: [Item1, Item5, Item3, Item6, Item2]
Final Content: [Newly added item, Item1, Item3, Item6, Item2]

Methods of LinkedList class:

For all the examples in the below methods, consider llistobj as a reference for LinkedList<String>.
LinkedList<String> llistobj  = new LinkedList<String>();
1) boolean add(Object item): It adds the item at the end of the list.
llistobj.add("Hello");
It would add the string “Hello” at the end of the linked list.
2) void add(int index, Object item): It adds an item at the given index of the the list.
llistobj.add(2, "bye");
This will add the string “bye” at the 3rd position( 2 index is 3rd position as index starts with 0).
3) boolean addAll(Collection c): It adds all the elements of the specified collection c to the list. It throws NullPointerException if the specified collection is null. Consider the below example –
LinkedList<String> llistobj = new LinkedList<String>();
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("String1");
arraylist.add("String2");
llistobj.addAll(arraylist);
This piece of code would add all the elements of ArrayList to the LinkedList.
4) boolean addAll(int index, Collection c): It adds all the elements of collection c to the list starting from a give index in the list. It throws NullPointerException if the collection c is null and IndexOutOfBoundsException when the specified index is out of the range.
llistobj.add(5, arraylist);
It would add all the elements of the ArrayList to the LinkedList starting from position 6 (index 5).
5) void addFirst(Object item): It adds the item (or element) at the first position in the list.
llistobj.addFirst("text");
It would add the string “text” at the beginning of the list.
6) void addLast(Object item): It inserts the specified item at the end of the list.
llistobj.addLast("Chaitanya");
This statement will add a string “Chaitanya” at the end position of the linked list.
7) void clear(): It removes all the elements of a list.
llistobj.clear();
8) Object clone(): It returns the copy of the list.
For e.g. My linkedList has four items: text1, text2, text3 and text4.
Object str= llistobj.clone();
 System.out.println(str);
Output: The output of above code would be:
[text1, text2, text3, text4]
9) boolean contains(Object item): It checks whether the given item is present in the list or not. If the item is present then it returns true else false.
boolean var = llistobj.contains("TestString");
It will check whether the string “TestString” exist in the list or not.
10) Object get(int index): It returns the item of the specified index from the list.
Object var = llistobj.get(2);
It will fetch the 3rd item from the list.
11) Object getFirst(): It fetches the first item from the list.
Object var = llistobj.getFirst();
12) Object getLast(): It fetches the last item from the list.
Object var= llistobj.getLast();
13) int indexOf(Object item): It returns the index of the specified item.
llistobj.indexOf("bye");
14) int lastIndexOf(Object item): It returns the index of last occurrence of the specified element.
int pos = llistobj.lastIndexOf("hello);
integer variable pos will be having the index of last occurrence of string “hello”.
15) Object poll(): It returns and removes the first item of the list.
Object o = llistobj.poll();
16) Object pollFirst(): same as poll() method. Removes the first item of the list.
Object o = llistobj.pollFirst();
17) Object pollLast(): It returns and removes the last element of the list.
Object o = llistobj.pollLast();
18) Object remove(): It removes the first element of the list.
llistobj.remove();
19) Object remove(int index): It removes the item from the list which is present at the specified index.
llistobj.remove(4);
It will remove the 5th element from the list.
20) Object remove(Object obj): It removes the specified object from the list.
llistobj.remove("Test Item");
21) Object removeFirst(): It removes the first item from the list.
llistobj.removeFirst();
22) Object removeLast(): It removes the last item of the list.
llistobj.removeLast();
23) Object removeFirstOccurrence(Object item): It removes the first occurrence of the specified item.
llistobj.removeFirstOccurrence("text");
It will remove the first occurrence of the string “text” from the list.
24) Object removeLastOccurrence(Object item): It removes the last occurrence of the given element.
llistobj.removeLastOccurrence("String1);
It will remove the last occurrence of string “String1″.
25) Object set(int index, Object item): It updates the item of specified index with the give value.
llistobj.set(2, "Test");
It will update the 3rd element with the string “Test”.
26) int size(): It returns the number of elements of the list.
llistobj.size();

Comments

Popular posts from this blog

Switch Case

Syntax : Switch Case in Java Programming It is alternative to else-if ladder. Switch Case Syntax is similar to – C/C++  Switch. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an  int, a String, or an enumerated value . switch (selection) { // value case value1 : // checking value 1 statement ( s ) ; break ; // use to break switch flow if condition match case value2 : // checking value 2 statement ( s ) ; break ; . . case value_n : statement ( s ) ; break ; default : statement ( s ) ; } Different Ways of Using Switch Case : Switch Case Using Integer Case int i=3; switch (i) { case 1 : System . out . println ( "One player is playing this game." ) ; break ; case 2 : System . out . println ( "Two players are playing ...

Inheritance in Java

Inheritance in Java Inheritance  is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class . The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class. Inheritance is a  compile-time  mechanism. A super-class can have any number of subclasses . But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have  “is-a”  relationship between them. Let’s have a look at the example below. Inheritance  Example Let’s consider a superclass  Vehicle . Different vehicles have different features and properties howeve...

Multilevel inheritance

Multilevel inheritance We discussed a bit about  Multilevel inheritance  in types of inheritance in java. In this tutorial we will explain multilevel inheritance with the help of   diagram  and  example program . It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance. Example : In this example we have three classes –  Car, Maruti and Maruti800. We have done a setup – class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car { ...