String str = "aaabbbcccdddabba"; HashSet<Character> hs = new HashSet<Character>(); for(Character ch:str.toCharArray()){ hs.add(ch); } System.out.println(hs);