Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Convert an ArrayList into a JSON String in Java?
Next article icon

How to Convert a LinkedHashSet into an Array and List in Java?

Last Updated : 15 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, LinkedHashSet is a predefined class of Java that extends the HashSet and it implements the Set interface. It can be used to implement the doubly linked list of the elements in the set, and it provides the predictable iteration order.

In this article, we will learn how to convert a LinkedHashSet into an Array and List in Java.

Step-by-Step Implementation to Convert a LinkedHashSet into an Array

  • Create the class named GfGLinkedHashSetToArray and write the main method into the class.
  • Create the one LinkedHashSet of the string instance which can store the string elements on it.
  • Add the elements on the LinkedHashSet using add() method.
  • Create the one-string array after that using toArray() method to convert the LinkedHashSet to an array.
  • Print the result conversion array.

Program to Convert a LinkedHashSet into an Array in Java

Below is the implementation of the Program to Convert a LinkedHashSet into an Array:

Java
// Java program to convert a LinkedHashSet into an Array import java.util.LinkedHashSet; import java.util.Arrays;  public class GfGLinkedHashSetToArray  {     public static void main(String[] args)      {         LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();         linkedHashSet.add("Java");         linkedHashSet.add("Spring Boot");         linkedHashSet.add("AWS");          // convert LinkedHashSet to Array         String[] array = linkedHashSet.toArray(new String[0]);          // print the array         System.out.println("Array: " + Arrays.toString(array));     } } 

Output
Array: [Java, Spring Boot, AWS]    

Explanation of the above Program:

The above program is the example program of the converting the LinkedHashSet into an array that can be implements the toArray pre-defined method.

  • add(): This is pre-defined method and it can be used to add the elements into the LinkedHashSet
  • toArray( ): This method is a pre-defined method that can be used to convert the another types into the array.

Step-by-Step Implementation to Convert a LinkedHashSet into a List

  • Create the class named GfGLinkedHashSetToList and write the main method into the class.
  • Create the one LinkedHashSet of the string instance which can stores the string elements on it.
  • Add the elements on the LinkedHashSet using add() method.
  • Create the one string list after that adding the LinkedHashSet into the list.
  • Print the result conversion list.

Program to Convert a LinkedHashSet into a List in Java

Below is the implementation of Program to Convert a LinkedHashSet into a List :

Java
// Java to convert a LinkedHashSet into a List import java.util.LinkedHashSet; import java.util.ArrayList; import java.util.List;  public class GfGLinkedHashSetToList {     public static void main(String[] args) {         LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();         linkedHashSet.add("C");         linkedHashSet.add("C++");         linkedHashSet.add("C#");          // Convert LinkedHashSet to List         List<String> list = new ArrayList<>(linkedHashSet);          // Print the list         System.out.println("List: " + list);     } } 

Output
List: [C, C++, C#]        

Explanation of the above Program:

  • The above program is the example program of the converting the LinkedHashSet into list that can be implements easily.
  • First create the instance of the LinkedHashSet after that adding the elements on the LinkedHashSet after that create the String instance of the list then add the LinkedHashSet into the list.

Next Article
How to Convert an ArrayList into a JSON String in Java?

K

kadambalamatclo
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Arrays
  • java-list
  • java-LinkedHashSet
  • Java Examples
Practice Tags :
  • Java

Similar Reads

  • How to Convert ArrayList to LinkedHashSet in Java?
    ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
    8 min read
  • How to Convert an ArrayList into a JSON String in Java?
    In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
    2 min read
  • Convert Array to LinkedList in Java
    Array is contiguous memory allocation while LinkedList is a block of elements randomly placed in the memory which are linked together where a block is holding the address of another block in memory. Sometimes as per requirement or because of space issues in memory where there are bigger chunks of co
    3 min read
  • How to Convert LinkedHashMap to List in Java?
    LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
    2 min read
  • How to Convert all LinkedHashMap Values to a List in Java?
    The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List
    2 min read
  • How to Convert an ArrayList or an Array into a TreeSet in Java?
    In Java, ArrayList allows random access because it works like an array. It means you can access any element by its index in O(1) at any time. A TreeSet is a sorted set implementation of the Set interface based on a TreeMap. It provides log(n) time complexity for basic operations like add, remove, an
    3 min read
  • Java Program to Convert an Array into a List
    In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
    4 min read
  • How to convert a Stream into a Map in Java
    Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. In this article, the methods to convert a stream into a map is discussed. Method 1: Using Collectors.t
    6 min read
  • Convert a HashSet to a LinkedList in Java
    In Java, HashSet and LinkedList are linear data structures. These are majorly used in many cases. There may be some cases where we need to convert HashSet to LinkedList. So, in this article, we will see how to convert HashSet to LinkedList in Java. Java Program to Convert a HashSet to a LinkedList T
    2 min read
  • How to Convert an ArrayList of Objects to a JSON Array in Java?
    In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences