- 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
How to convert an Array to a Set in Java?
One solution to add an array to set is to use the addAll() method of the Collections class. This method accepts a collection and an element and, it adds the given element to the specified Collection.
Example
Live Demoimport java.util.Collections; import java.util.HashSet; import java.util.Set; public class ArrayToSet { public static void main(String args[]) { Integer[] myArray = {23, 93, 56, 92, 39}; Set<Integer> set = new HashSet<Integer>(); Collections.addAll(set, myArray); System.out.println(set); } }
Output
[23, 39, 56, 92, 93]
- Related Articles
- Java program to convert a Set to an array
- Java program to convert an Array to Set
- Java Program to convert a set into an Array
- How to convert an array to Set and vice versa in Java?
- Program to convert Array to Set in Java
- How to convert an array to a list in Java?
- How to convert an object array to an integer array in Java?
- How to convert integer set to int array using Java?
- How to convert an array to string in java?
- How to convert an Array to a List in Java Program?
- How to Convert a Java 8 Stream to an Array?
- Convert a Vector to an array in Java
- How to convert an object to byte array in java?
- How to convert Array to Set in JavaScript?
- Convert LinkedList to an array in Java

Advertisements