Java Program to Get the First Element from LinkedHashSet Last Updated : 24 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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. Converting it to an array or ListUsing IteratorsUsing 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]); } } } OutputFirst 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()); } } } OutputFirst 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); } } } OutputFirst element in LinkedHashSet: 1 Comment More infoAdvertise with us Next Article Java Program to Get the First Element from LinkedHashSet S sub154 Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2020 Java-Collections java-LinkedHashSet +1 More Practice Tags : JavaJava-Collections Similar Reads LinkedHashSet isEmpty() method in Java The Java.util.LinkedHashSet.isEmpty() method is used to check if a LinkedHashSet is empty or not. It returns True if the LinkedHashSet is empty otherwise it returns False. Syntax: Linked_Hash_Set.isEmpty() Parameters: This method does not take any parameter Return Value: The function returns True if 1 min read LinkedList element() Method in Java In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem 2 min read LinkedHashSet contains() method in Java The Java.util.LinkedHashSet.contains() method is used to check whether a specific element is present in the LinkedHashSet or not. So basically it is used to check if a Set contains any particular element. Syntax: Hash_Set.contains(Object element) Parameters: The parameter element is of the type of L 2 min read LinkedHashSet equals() method in Java with Example The equals() method of java.util.LinkedHashSet class is used to compare the specified object with this set for equality. Returns true if and only if the specified object is also a set, both sets have the same size, and all corresponding pairs of elements in the two sets are equal. (Two elements e1 a 2 min read LinkedList getFirst() Method in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and addr 2 min read Like