如何从字符串哈希集中创建字符数组?
How to create an array of characters from a hashset of strings?
我正在尝试为我的 class 构建一个 Vigenere 解密程序。这些说明要求程序能够解密多种语言。因此,我需要找出如何遍历字符串哈希集并创建包含在这些字符串中的字符数组以及每个字符出现的次数。我已经尝试了很长一段时间,但我写的任何东西都不起作用。 `
public char mostCommonCharln(HashSet<String> dictionary) {
for (String s : dictionary) {
//what do I write here??? //
return Characters;
}
}
我假设您想要的签名是:
public static List<CharFrequency> mostCommonChars(Set<String> dictionary)
其中 CharFrequency
class 定义为:
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;
}
}
然后你就会有下面的方法:
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;
}
这不是很有效,我没有尝试不同的输入就写了它,但它可以作为你的开始。
最终答案:
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;
}
我正在尝试为我的 class 构建一个 Vigenere 解密程序。这些说明要求程序能够解密多种语言。因此,我需要找出如何遍历字符串哈希集并创建包含在这些字符串中的字符数组以及每个字符出现的次数。我已经尝试了很长一段时间,但我写的任何东西都不起作用。 `
public char mostCommonCharln(HashSet<String> dictionary) {
for (String s : dictionary) {
//what do I write here??? //
return Characters;
}
}
我假设您想要的签名是:
public static List<CharFrequency> mostCommonChars(Set<String> dictionary)
其中 CharFrequency
class 定义为:
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;
}
}
然后你就会有下面的方法:
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;
}
这不是很有效,我没有尝试不同的输入就写了它,但它可以作为你的开始。
最终答案:
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;
}