/** * @author lautturi.com * Java example: converting array to set in java */ import java.security.SecureRandom; import java.util.*; import com.google.common.collect.Sets; public class Lautturi { public static void main(String[] args){ Integer[] arr1 = {1, 3, 5, 2, 4}; Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1)); String[] arr2 = {"hello","lautturi","java","python","world","lau"}; // with google guava Set<String> set2 = Sets.newHashSet(arr2); System.out.println("Set1:"+set1); System.out.println("Set2:"+set2); } }Sow:ecruww.lautturi.com
output:
Set1:[1, 2, 3, 4, 5] Set2:[lau, python, java, world, lautturi, hello]