Java Program to Get the First Element from LinkedHashSet


LinkedHashSet is a class provided by Java that implements Set Interface. The first element of a LinkedhashSet is nothing but the first element in the collection. In this article, we will discuss different approaches to get the first element from LinkedHashset.

What is a LinkedHashSet?

A LinkedHashSet is a collection which is a combination of HashSet collection and a LinkedList collection. A HashSet is an unordered collection and also doesn’t allow duplicates. A LinkedList stores duplicates but also is an ordered collection i.e., it stores the elements in a way in which add elements to the collection. Coming to LinkedHashSet as it is a combination of both hashSet and LinkedList it stores the elements in the same way we add elements to the collection i.e., it is ordered and it also doesn’t allow duplicates to store elements in the collection. Now, let us look at a few basic operations that can be performed on LinkeHashSet.

Basic Operations on LinkedHashSet

Here, we will discuss regarding the basic operations that we can perform on LinkedHashSet using in built methods provided by Java LinkedHashSet Class.

add() − This method helps to add elements to the LinkedHashSet.It accepts a parameter basically the type of elements stored in the LinkedHashSet.

linkedHashSetObject.add(element)

remove() − This method helps to remove elements from the LinkedHashSet. The element passed to this method is removed from the LinkedHashSet.

LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("o");
set.remove("b"); // removes "b" from the set
System.out.println(set); // output: ["a", "o"]

clear() − This method helps to clear all elements from the LinkedHashSet.

LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("o");
set.clear(); // removes all elements from set
System.out.println(set); // prints empty set 

contains() − This method helps to check whether an element which is passed as a parameter is present in the LinkedHashSet. It returns boolean value. If the element is present it returns true else returns false.

LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("o");
boolean val = set.contains(‘b’); // checks ‘b’ is present and returns boolean value. System.out.println(val); //  prints true

isEmpty() − This method helps to check whether the LinkedHashSet is empty or not. It returns boolean value. If the hashset is empty is it returns true else returns false.

LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("o");
boolean val = set.isEmpty(); // checks hashSet conatians elements or not  System.out.println(val); //  prints false

Syntax

Creating an instance of LinkedHashSet −

LinkedHashSet<datatype> set = new LinkedHashSet<>();

iterator() − It iterates over the Linkedhashset and returns the values.

Iterator<datatype> iteratorobjectname = setobject.iterator()

hasNext() − This method is used check whether next element is present in the collection. It is provided by the Iterator Interface in java. It is called on iterator Object.

iteraratorObject.hasNext()

next() − This method helps us to return the next element present in the collection. It is provided by the Iterator Interface in java. It is called on iterator Object.

iteraratorObject.next()

toArray() − This method is used to convert a collection into an array. It returns an array of elements.

collectionobject.toArray()

Now, we will be discussing in detail through java codes how to find the first element from a LinkedHashSet.

Approach 1: Using iterator() method

In this approach, we will use the iterator() method and find the first element in LinkedHashSet.

  • Create a LinkedHashSet instance and add elements to the set using the add() method.

  • Create an iterator instance and retrieve the first element using next() method.

  • Print the first element of LinkedHashSet.

Example

In this example, we create a LinkedhashSet object and then add a few elements to LinkedHashSet object using ‘add()’ method and then we use the iterator() constructor to create an iterator object on created LinkedhashSet object. We then use the next() method to get the first element of the set and print the value.

import java.util.*;

public class Main {
   public static void main(String[] args) {
      LinkedHashSet<Integer> hm = new LinkedHashSet<>();
      hm.add(12);
      hm.add(20);
      hm.add(35);
      Iterator<Integer> iterator = hm.iterator();
      int first= iterator.next();
      System.out.println("First element of HashSet: " + first);          
   }
}

Output

First element of HashSet: 12

Approach 2: Using toArray() method

In this approach, we will convert a set to an array using ‘toArray()’ method and then we will find the first and last element of LinkedHashSet.

  • Create a LinkedHashSet instance and add elements to the set using the add() method.

  • Convert the set into an array object using the toArray() method.

  • Retrieve the first element of the array using index and print it.

Example

In this example, we create a LinkedHashSet object and then add a few elements to the LinkedhashSet variable and then we covert this set to array using toArray() method and then we access the first element of the array index and print the value of it.

import java.util.LinkedHashSet;

public class Main {
   public static void main(String[] args) {
      LinkedHashSet<String> hm = new LinkedHashSet<>();
      hm.add("apple");
      hm.add("banana");
      hm.add("cherry");
      // Convert the set to an array
      String[] arr = new String[hm.size()];
      hm.toArray(arr); 
      // Find the first element of the array
      String first = arr[0];        
      System.out.println("First element: " + first);
   }
}

Output

First element: apple

Thus, in this article we have discussed about different approaches of finding first element from a LinkedHashSet using Java Programming Language.

Updated on: 16-Aug-2023

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements