Open In App

Java Program to Get the First Element from LinkedHashSet

Last Updated : 24 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

LinkedHashSet is an implementation of Set Abstract Data Type (ADT). It extends from the HashSet class which in-turn implements Set Interface. The difference between the LinkedHashSet and HashSet is the property of maintaining the element ordering. LinkedList is just a container holding sequence of elements. By definition, Set should not contain duplicate elements.  This property need not hold good for the LinkedList ADT. When elements should be duplicated and the order has to be maintained, one should use LinkedHashSet.

Methods: 

There are basically three standard ways to get an element from LinkedHashSet without changing it to a different data structure.

  1. Converting it to an array or List
  2. Using Iterators
  3. Using Streams 

Implementation: Getting the First Element from LinkedHashSet

Method 1: By converting it to an array or List

Example

Java




// Java Program to Get the First Element from LinkedHashSet
// by converting it to an array or List
// Array is demonstrated below so do with List
 
// Importing generic java packages
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Class
public class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashMap object
        // Declaring object of Integer type
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashMap
        hashSet.add(2);
        hashSet.add(1);
        hashSet.add(4);
        hashSet.add(6);
        hashSet.add(8);
 
        // Condition check using isEmpty() method which
        // holds
 
        // True till there is a single element in an object
        // is remaining False, when there is no object left
        // or if initially there was no element added
        if (!hashSet.isEmpty()) {
            // Converting the above Map to an array
            Integer arr[] = new Integer[hashSet.size()];
            arr = hashSet.toArray(arr);
 
            // Accessing the first element by passing 0
            // as an argument which by default
            // accesses and prints out first element
            System.out.println("First element: " + arr[0]);
        }
    }
}


Output

First element: 2

Method 2: Using Iterators

 Example 

Java




// Java Program to Get the First Element from LinkedHashSet
// Using Iterators
 
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
 
// Class
public class GFG {
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Creating a LinkedHashMap
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashMap
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Iterator over LinkedHashMap
        Iterator<Integer> iter = hashSet.iterator();
 
        if (iter != null && iter.hasNext()) {
            // Display the first element of Map using next()
            // ethod
            System.out.println(
                "First element in LinkedHashSet: "
                + iter.next());
        }
    }
}


Output

First element in LinkedHashSet: 1

Method 3: Using Streams 

Example: 

Java




// Java Program to Get the First Element from LinkedHashSet
// Using Streams
 
// Importing generic java packages
import java.util.*;
import java.lang.*;
import java.io.*;
 
// Class
class GFG {
   
    // Main driver method
    public static void main(String[] args) throws java.lang.Exception
    {
        // Creating a LinkedHashMap
        LinkedHashSet<Integer> hashSet
            = new LinkedHashSet<Integer>();
 
        // Adding elements to LinkedHashMap
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(3);
        hashSet.add(4);
        hashSet.add(5);
 
        // Checking whether Map is empty or not
        if (hashSet.size() == 0)
 
            // Display message
            System.out.println("The Set is Empty!");
        else {
            // Using stream() through findFirst() method
            // over the elements of LinkedHashMap
            int first = hashSet.stream().findFirst().get();
 
            // Printing the first element of LinkedHashMap
            System.out.println(
                "First element in LinkedHashSet: " + first);
        }
    }
}


Output

First element in LinkedHashSet: 1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads