- 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
Convert array to HashSet in Java
Create an array and convert it to List −
Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List<Integer> l = Arrays.asList(arr);
Now, let us convert the above array to HashSet −
Set<Integer> s = new HashSet<Integer>(l);
The following is an example to convert array to HashSet −
Example
import java.util.*; public class Demo { public static void main(String[] argv) throws Exception { Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List<Integer> l = Arrays.asList(arr); Set<Integer> s = new HashSet<Integer>(l); for (Iterator i = s.iterator(); i.hasNext();) { Object ele = i.next(); System.out.println(ele); } } }
Output
35 20 40 10 30 15
- Related Articles
- Convert elements in a HashSet to an array in Java
- Convert HashSet to TreeSet in Java
- Convert an ArrayList to HashSet in Java
- Java Program to convert HashSet to Enumeration
- C++ Program to Convert Array to Set (Hashset)
- Haskell Program to Convert Array to Set (HashSet)
- Copy all elements of Java HashSet to an Object Array
- HashSet in Java
- convert list to array in java
- Add elements to HashSet in Java
- How to sort HashSet in Java
- Initialize HashSet in Java
- Initializing HashSet in Java
- The HashSet in Java
- how to convert Object array to String array in java

Advertisements