Java examples for java.util:Set Creation
Convert Array of string as Immutable Set
//package com.java2s; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] argv) { String[] names = new String[] { "1", "abc", "level", null, "java2s.com", "asdf 123" }; System.out.println(asImmutableSet(names)); }/* www.j av a2 s .c o m*/ public static Set<String> asImmutableSet(String[] names) { //The intention here is to save some memory by picking the simplest safe representation, // as we usually require immutable sets for long living metadata: if (names == null || names.length == 0) { return Collections.<String> emptySet(); } else if (names.length == 1) { return Collections.singleton(names[0]); } else { HashSet<String> hashSet = new HashSet<>(Arrays.asList(names)); return Collections.unmodifiableSet(hashSet); } } }