
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get First Element from LinkedHashSet in Java
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 which doesn't allow duplicates. But, a LinkedList is an ordered collection which stores duplicates
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 duplicate elements in the collection.
Creating an instance of LinkedHashSet
Below is the syntax to create an instance of LinkedHashSet ?
LinkedHashSet<datatype> set = new LinkedHashSet<>();
Basic Operations on LinkedHashSet
Here, we will discuss regarding the basic operations that we can perform on LinkedHashSet using inbuilt 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.
- remove(): This method helps to remove elements from the LinkedHashSet. The element passed to this method is removed from the LinkedHashSet.
- clear(): This method helps to clear all elements from the LinkedHashSet.
- 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.
- 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.
- iterator(): It iterates over the LinkedhashSet and returns the values.
- 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.
- 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.
- toArray(): This method is used to convert a collection into an array. It returns an array of elements.
Finding First Element 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
Following is the practical implementation of above approach.
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 of the above is ?
First element of HashSet: 12
Finding First Element 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
Here, we use toArray() method to find the first element from LinkedhashSet.
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 obtained as ?
First element: apple