迭代时,哈希表键会重新排序

我正在尝试学习Java并进行了一项练习,该练习是获取字符串中的第一个唯一项。 我使用HashTable作为字符串的字符作为键,并使用计数器将其在字符串中看到多少次作为值。

After that, I'm iterating over the HashTable and checking for each key if it has a value of 1 (only seen once) I want to return it. The problem is, when I have a string like this for example abcdcd a and b are both unique, but a is the first one, but for some reason, when I get the keys on the HashTable, the b comes before a.

这就是我所拥有的:

    public static Character firstNonRepeatedCharacter(String str) {
    Hashtable<Character, Integer> characters = new Hashtable<Character, Integer>();
    char[] charsInString = str.toCharArray();

    for(int i=0; i<charsInString.length; i++){
        if (characters.containsKey(charsInString[i])){
            int counter = characters.get(charsInString[i]);
            counter ++;
            characters.put(charsInString[i], counter);
        }else {
            characters.put(charsInString[i], 1);
        }
    }

    Enumeration<Character> enumeration = characters.keys();
    while(enumeration.hasMoreElements()) {
        Character key = enumeration.nextElement();
        if(characters.get(key) == 1) {
            return key;
        }
    }

    return null;

}

为什么会这样呢?