- 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]
Advertisements