如何从字符串的hashset创建一个字符数组?

时间:2016-04-16 19:31:03

标签: java encryption hashset

我正在尝试为我的班级构建一个Vigenere解密程序。该指令要求程序能够解密多种语言。因此,我需要找出如何迭代字符串的哈希集并创建包含在这些字符串中的字符数组以及每个字符出现的次数。我已经尝试了很长一段时间,我写的任何东西都没有用。 `

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

2 个答案:

答案 0 :(得分:1)

我将假设您想要的签名是:

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

CharFrequency类定义为:

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

这不是很有效率,我在不尝试不同输入的情况下编写了它,但它可以作为一个开始。

答案 1 :(得分:0)

最终答案:

    <div ng-repeat="clothing in main.usersClothing">
        <img ng-src="{{clothing.image}}" class="clothing-img">
    </div>