使用散列图删除重复值时删除大写字母

时间:2017-10-28 13:24:38

标签: java hashmap

如何删除大写字母?

我删除了小写重复项,但如果是大写的话我无法删除副本。

import java.util.HashMap;

public class HashMapDemo {

    public String removeDupes(String str){

        HashMap<Character, Integer> map = new HashMap<Character, Integer>();

        for(int i = 0; i < str.length(); i ++){
            char c = str.charAt(i);
            String s = String.valueOf(c);
            char d = (s.toUpperCase()).charAt(0);
            if(map.containsKey(c) || map.containsKey(d)){
                int count = map.get(c);
                map.put(c, count++);
            }
            else{
                map.put(c, 1);
            }
        }

        for(Character key : map.keySet()){
            int placeHolder = map.get(key);
            if(placeHolder != 1){
                map.remove(key);
            }
        }
        char[] placeHolder = new char[str.length()];
        int i = 0;
        for(Character key: map.keySet()){
            char var = key;
            placeHolder[i] = var;
            i++;
        }
        String lastWord = String.valueOf(placeHolder);

        return lastWord;
    }
}

0 个答案:

没有答案