Java Collections Framework:LinkedList-3(Get/Search in LinkedList)

 

Java – Get first and last elements from LinkedList example


In this tutorial we will see an example on how to get first and last element from LinkedList.

Example

Here we have a LinkedList of String type and we are getting first and last element from it using getFirst() and getLast() methods of LinkedList class. Method definition and description are as follows:
1) public E getFirst(): Returns the first element in this list.
2) public E getLast(): Returns the last element in this list.

import java.util.LinkedList;
public class GetFirstAndLast {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Item1");
    linkedlist.add("Item2");
    linkedlist.add("Item3");
    linkedlist.add("Item4");
    linkedlist.add("Item5");
    linkedlist.add("Item6");
 
    // Getting First element of the List
    Object firstElement = linkedlist.getFirst();
    System.out.println("First Element is: "+firstElement);
 
    // Getting Last element of the List
    Object lastElement = linkedlist.getLast();
    System.out.println("Last Element is: "+lastElement);
 }
}

Output:

First Element is: Item1
Last Element is: Item6

Java – Get element from specific index of LinkedList example


In this example we are gonna see how to get an element from specific index of LinkedList using get(int index) method:

public E get(int index): Returns the element at the specified position in this list.

import java.util.LinkedList;

public class GetElementExample {
 public static void main(String[] args) {
 
    // Creating LinkedList of String Elements
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Populating it with String values
    linkedlist.add("AA");
    linkedlist.add("BB");
    linkedlist.add("CC");
    linkedlist.add("DD");
    linkedlist.add("EE");

    System.out.println("LinkedList Elements : ");
    //get(i) returns element present at index i
    for(int i=0; i < linkedlist.size(); i++){
      System.out.println("Element at index "+i+" is: "+linkedlist.get(i));
    } 
 }
}

Output:

LinkedList Elements : 
Element at index 0 is: AA
Element at index 1 is: BB
Element at index 2 is: CC
Element at index 3 is: DD
Element at index 4 is: EE

Java – Search elements in LinkedList example


In this tutorial we will learn how to search elements in LinkedList. We will be using following two methods for searching elements.

public int indexOf(Object o): Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

public int lastIndexOf(Object o): Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Example

Here we are having a LinkedList of String elements and we are searching a string using indexOf() and lastIndexOf() methods of LinkedList class.


import java.util.LinkedList;
public class SearchInLinkedList {
 
  public static void main(String[] args) {
 
     // Step1: Create a LinkedList
     LinkedList<String> linkedlist = new LinkedList<String>();
 
     // Step2: Add elements to LinkedList
     linkedlist.add("Tim");
     linkedlist.add("Rock");
     linkedlist.add("Hulk");
     linkedlist.add("Rock");
     linkedlist.add("James");
     linkedlist.add("Rock");
 
     //Searching first occurrence of element
     int firstIndex = linkedlist.indexOf("Rock");
     System.out.println("First Occurrence: " + firstIndex);
 
     //Searching last occurrence of element
     int lastIndex = linkedlist.lastIndexOf("Rock");
     System.out.println("Last Occurrence: " + lastIndex);
  }
}

Output:

First Occurrence: 1
Last Occurrence: 5

Reference:
LinkedList JavaDoc

Java – Get sub List from LinkedList example


Example

In this example, we are getting a sublist of LinkedList using subList(int startIndex, int endIndex) method of LinkedList class. It returns a List between the specified index startIndex(inclusive) and endIndex(exclusive). Any changes made to the sublist will be reflected in the original list (We have tested this in the below program by removing an element from sublist and displaying original list after remove).

import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
public class SublistExample {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Item1");
    linkedlist.add("Item2");
    linkedlist.add("Item3");
    linkedlist.add("Item4");
    linkedlist.add("Item5");
    linkedlist.add("Item6");
    linkedlist.add("Item7");
 
    // Displaying LinkedList elements
    System.out.println("LinkedList elements:");
    Iterator it= linkedlist.iterator();
    while(it.hasNext()){
       System.out.println(it.next());
    }
 
    // Obtaining Sublist from the LinkedList
    List sublist = linkedlist.subList(2,5);
 
    // Displaying SubList elements
    System.out.println("\nSub List elements:");
    Iterator subit= sublist.iterator();
    while(subit.hasNext()){
       System.out.println(subit.next());
    }
 
    /* Any changes made to Sub List will be reflected
     * in the original List. Lets take this example - We
     * are removing element "Item4" from sublist and it 
     * should be removed from original list too. Observe 
     * the Output of this part of the program.
     */
    sublist.remove("Item4");
    System.out.println("\nLinkedList elements After remove:");
    Iterator it2= linkedlist.iterator();
    while(it2.hasNext()){
       System.out.println(it2.next());
    }
 }
}

Output:

LinkedList elements:
Item1
Item2
Item3
Item4
Item5
Item6
Item7

Sub List elements:
Item3
Item4
Item5

LinkedList elements After remove:
Item1
Item2
Item3
Item5
Item6
Item7


Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java Basics:Data Types