- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy all elements of Java LinkedHashSet to an Object Array
First, create a LinkedHashSet and add elements −
LinkedHashSet<String> l = new LinkedHashSet<String>(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));
Now, copy it to an object array like this −
// copying Object[] arr = l.toArray();
The following is an example to copy all elements of a LinkedHashSet to an object array −
Example
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet<String> l = new LinkedHashSet<String>(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7")); System.out.println("LinkedHashSet elements..."); System.out.println(l); // copying Object[] arr = l.toArray(); System.out.println("Object Array: "); for (Object res: arr){ System.out.println(res); } } }
Output
LinkedHashSet elements... [1, 2, 3, 4, 5, 6, 7] Object Array: 1 2 3 4 5 6 7
- Related Articles
- Copy all elements of Java HashSet to an Object Array
- Copy all elements of ArrayList to an Object Array in Java
- Copy all elements in Java TreeSet to an Object Array
- Remove all elements from Java LinkedHashSet
- Iterate through elements of Java LinkedHashSet
- Python program to copy all elements of one array into another array
- Swift Program to Copy All the Elements of One Array to Another Array
- C++ Program to Copy All the Elements of One Array to Another Array
- Golang Program To Copy All The Elements Of One Array To Another Array
- Create an object array from elements of LinkedList in Java
- Delete All Odd Elements from an Array in Java
- Copy all the elements from one set to another in Java
- Copy and return all the elements of a masked array in Numpy
- Java Program to copy an array from the specified source array
- How to copy a specific section of an array in Java?

Advertisements