- 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
Program to convert Array to Set in Java
Let’s say the following is our array of strings −
String arr[] = { "Andy", "John", "Kevin", "Mary", "Ryan" };
Now, convert the array to set with HashSet −
Set<String>set = new HashSet<>(Arrays.asList(arr));
Example
Following is the program to convert Array to Set in Java −
import java.util.*; public class Demo { public static void main(String[] args) { String arr[] = { "Andy", "John", "Kevin", "Mary", "Ryan" }; System.out.println("Array = "+ Arrays.toString(arr)); Set<String>set = new HashSet<>(Arrays.asList(arr)); System.out.println("Set = " + set); } }
Output
Array = [Andy, John, Kevin, Mary, Ryan] Set = [Kevin, Ryan, John, Andy, Mary]
- Related Articles
- Java program to convert an Array to Set
- Java program to convert a Set to an array
- Java Program to convert a set into an Array
- Program to convert set of Integer to Array of Integer in Java
- Swift Program to Convert Array to Set
- Golang Program To Convert Array To Set
- Program to convert Set to List in Java
- C++ Program to Convert Array to Set (Hashset)
- Haskell Program to Convert Array to Set (HashSet)
- Swift Program to Convert Set into Array
- How to convert an Array to a Set in Java?
- Program to convert Array to List in Java
- Program to convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Program to convert Primitive Array to Stream in Java

Advertisements