How do I convert array to Set?

Bookmark this example!  
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!
Firefox 2
Google

100 Top & Latest

GetJava Download Button

Locations of visitors to this page
eXTReMe Tracker
visitor stats