Java examples for java.util:Set Creation
Convert String To Unmodifiable Set<Character>
//package com.java2s; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] argv) { String str = "java2s.com"; System.out.println(strToUnmodifiableSet(str)); }/* w w w . jav a 2s.c om*/ public static Set<Character> strToUnmodifiableSet(final String str) { if (str == null) { return Collections.emptySet(); } if (str.length() == 1) { return Collections.singleton(str.charAt(0)); } return Collections.unmodifiableSet(strToSet(str)); } private static Set<Character> strToSet(final String str) { Set<Character> set; if (str == null) { return new HashSet<Character>(); } set = new HashSet<Character>(str.length()); for (int i = 0; i < str.length(); i++) { set.add(str.charAt(i)); } return set; } }