Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to Get the Last Element from LinkedHashSet in Java?

How to Get the Last Element from LinkedHashSet in Java?

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

There are two ways by which we can get the last element from LinkedHashSet:

  1. By iterating the Set.
  2. By converting the LinkedHashSet to array or list.

Method 1: By iterating the Set

By iterate LinkedHashSet: To get the last element from LinkedHashSet by iterate LinkedHashSet, the process divided into two parts:

  • First, make a variable lastEle
  • Iterate LinkedHashSet and get the last element

Example:

Java




// Java program to find the last 
// element from LinkedHashSet
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(10);
        set.add(50);
        set.add(20);
  
        // Last element
        int lastEle = 0;
  
        // Iterate LinkedHashSet
        for (int x : set)
        {
            lastEle = x;
        }
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print the last element of the LinkedHashSet
        System.out.println("Last element of LinkedHashSet: "
                           + lastEle);
    }
}


Output

LinkedHashSet: [10, 20, 30, 50]
Last element of LinkedHashSet: 50

Method 2: By converting the LinkedHashSet to Array

To get the last element from LinkedHashSet using Array, the process divided into three parts:

  • First, make an Array.
  • Convert LinkedHashSet to Array with the help of toArray() method.
  • Get the last element using the last index.

Example:

Java




// Java program to find the last 
// element from LinkedHashSet
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(10);
        set.add(50);
        set.add(20);
  
        Integer[] elements = new Integer[set.size()];
  
        // Convert LinkedHashSet to Array
        elements = set.toArray(elements);
  
        // Get the last element with the help of the index.
        int lastEle = elements[elements.length - 1];
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print the last element of the LinkedHashSet
        System.out.println("Last element of LinkedHashSet: "
                           + lastEle);
    }
}


Output

LinkedHashSet: [10, 20, 30, 50]
Last element of LinkedHashSet: 50
RELATED ARTICLES

Most Popular

Recent Comments