Convert String To Set<Character> - Java java.util

Java examples for java.util:Set Creation

Description

Convert String To Set<Character>

Demo Code


//package com.java2s;

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(strToSet(str));
    }//  ww  w .j a  va2s.co m

    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;
    }
}

Related Tutorials