String str = "i am a string";
System.out.println(new HashSet<String>(Arrays.asList(str.split(""))));
EDIT: for those who object that they are not completely equivalent, since str.split will contain an empty string in the set, we can make this even more verbose:
String str = "i am a string";
Set<String> set = new HashSet<String>(Arrays.asList(str.split("")));
set.remove("");
System.out.println(set);
But of course, it depends on what you need to accomplish.
source
share