Converts an array of chars to a Set of Characters. - Java java.util

Java examples for java.util:Set Creation

Description

Converts an array of chars to a Set of Characters.

Demo Code


//package com.java2s;

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

public class Main {
    public static void main(String[] argv) {
        char array = 'a';
        System.out.println(arrayToSet(array));
    }/*w ww  .j a  v  a  2 s. c  o  m*/

    /**
     * Converts an array of chars to a Set of Characters.
     *
     * @param array the contents of the new Set
     * @return a Set containing the elements in the array
     */
    public static Set<Character> arrayToSet(char... array) {
        Set<Character> toReturn;

        if (array == null) {
            return new HashSet<Character>();
        }
        toReturn = new HashSet<Character>(array.length);
        for (char c : array) {
            toReturn.add(c);
        }
        return toReturn;
    }
}

Related Tutorials