Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to convert String to a unique Set of Characters in Java

3 Answers

0 votes
import java.util.*; 
import java.util.stream.Collectors;

public class MyClass {
    public static Set<Character> convertStringToCharList(String s) { 
      List<Character> list = new ArrayList<Character>();
      Set<Character> uniqueSet = new HashSet<Character>();
      for (char ch : s.toCharArray()) {
            list.add(ch);
            uniqueSet.add(ch);
      }
  
      return uniqueSet; 
    } 
    public static void main(String args[]) {
      String s = "java programming"; 
  
      Set<Character> charSet = convertStringToCharList(s); 

      System.out.println(charSet);
    }
}



/*
run:

[ , p, a, r, v, g, i, j, m, n, o]

*/

 



answered May 18, 2020 by avibootz
0 votes
import java.util.*; 
import java.util.stream.Collectors;
 
public class MyClass {
    public static Set<Character> convertStringToCharList(String s) { 
      Set<Character> charSet = s.chars().mapToObj(e->(char)e)
                                          .collect(Collectors.toSet());
      return charSet; 
    } 
    public static void main(String args[]) {
      String s = "java programming"; 
   
      Set<Character> charSet = convertStringToCharList(s); 
 
      System.out.println(charSet);
    }
}
 
 
 
/*
run:
 
[ , p, a, r, v, g, i, j, m, n, o]
 
*/

 



answered May 18, 2020 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static void main(String args[]) {
      String s = "java programming"; 
      Set<Character> charSet = new HashSet<Character>();
   
      for (char ch : s.toCharArray()) {
            charSet.add(ch);
      }
 
      System.out.println(charSet);
    }
}
 
 
 
/*
run:
 
[ , p, a, r, v, g, i, j, m, n, o]
 
*/

 



answered May 18, 2020 by avibootz

Related questions

...