Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**
     * Convert a String to a unmodifiable set of characters.
     * @param str The string to convert
     * @return A set containing the characters in str. A empty set
     *    is returned if str is null.
     */
    public static Set<Character> strToUnmodifiableSet(String str) {
        if (str == null)
            return Collections.emptySet();
        if (str.length() == 1)
            return Collections.singleton(str.charAt(0));
        return Collections.unmodifiableSet(strToSet(str));
    }

    /**
     * Convert a String to a set of characters.
     * @param str The string to convert
     * @return A set containing the characters in str. A empty set
     *    is returned if str is null.
     */
    public static Set<Character> strToSet(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;
    }
}