Ryan Ryan - 6 months ago 22x
Java Question

How to create an array of characters from a hashset of strings?

I am trying to build a Vigenere decryption program for my class. The instructions require the program be able to decrypt for multiple languages. So, I need to find out how to iterate over a hashset of strings and create an array of characters contained in those strings as well as number of times each character occurs. I've been trying for quite a while now and nothing I write is working. `

public char mostCommonCharln(HashSet<String> dictionary) {
for (String s : dictionary) {
//what do I write here??? //
return Characters;


I will assume the signature that you want is:

public static List<CharFrequency> mostCommonChars(Set<String> dictionary)

Where CharFrequency class is defined as:

class CharFrequency implements {

    private char value;
    private int count;

    public CharFrequency(char v, int c) {
        this.value = v;
        this.count = c;

    public String toString() {
        return value + " -> " + count;

And then you will have the following method:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

public static List<CharFrequency> mostCommonChars(Set<String> dictionary) {
    // Concat all strings present in dictionary into a big string
    String allchars = dictionary.stream().collect(Collectors.joining());
    // Then convert it to a List<Character> which can use Java Streams
    List<Character> charList = new ArrayList<>(allchars.length());
    for (char c : allchars.toCharArray()) {

    return charList
            // Group all chars by the value of itself, which
            // will result in a Map<Char, List<Char>>
            .collect(Collectors.groupingBy(item -> item))
            // Maps the Map<Char, List<Char>> collected above in a
            // CharFrequency using the List<Char>.size as the repetition
            // count of that char
            .map(entry -> new CharFrequency(entry.getKey(), entry.getValue().size()))

This is not very efficient and I've written it without trying different inputs, but it could serve as a start to you.