简体   繁体   中英

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

    @Override
    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;
import java.util.function.Function;

    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()) {
        charList.add(c);
    }

    final List<CharFrequency> result = new ArrayList<>();
    charList.stream()
            // Group by the char itself and count occurrences
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .forEach((character, count1) -> result.add(new CharFrequency(character, count1)));
    return result;
}

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

The final answer:

public char mostCommonCharln(HashSet<String> dictionary){
    StringBuilder sb= new StringBuilder();
    //String allChars = sb.toString();
    String alph = "abcdefghijklmnopqrstuvwxyz";
    int[] counts= new int[26];
    String allChars =dictionary.stream().collect(Collectors.joining());
    for (int k = 0; k<allChars.length();k++){
        char ch = Character.toLowerCase(allChars.charAt(k));
        int dex = alph.indexOf(ch);
        if(dex != -1){
            counts[dex]+=1;
        }
    }
    int maxDex = 0;
    for (int i=0;i<counts.length;i++){
        if (counts[i]>counts[maxDex]){
            maxDex=i;
        }
    }
    char mostFreq=alph.charAt(maxDex);
    System.out.println (mostFreq);
    return mostFreq;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM