How do I convert array to Set?
Category: java.util, viewed: 2464 time(s).
package org.kodejava.example.util; import java.util.*; public class ArrayToSetExample { public static void main(String[] args) { Integer[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4}; // // To convert an array into a Set first we convert it to a List. Next // with the list we create a HashSet and pass the list as the constructor. // List list = Arrays.asList(numbers); Set set = new HashSet(list); // // Display what we get in the set. // for (Iterator iterator = set.iterator(); iterator.hasNext();) { Object o = iterator.next(); System.out.print(o + ", "); } } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I sort items of an ArrayList?
- How do I sort array values in case insensitive order?
- How do I sort array values in descending order?
- How do I convert milliseconds value to date?
- How do I split a string using Scanner class?
- How do I read file using Scanner class?
- How do I read / write data in Windows registry?
- How do I read user input from console using Scanner class?
- How do I use ResourceBundle for i18n?
- How do I convert time between timezone?