//对你的hashMap解法测试了下子 import java.util.*; public class MapTest{ public static void getChar(String str) { char[]chas=str.toCharArray(); HashMap<Character,Integer>hash=new HashMap<>(); int key=1; for(int i=0;i<chas.length;i++) { if(hash.containsKey(chas[i])) { int v=hash.get(chas[i]); hash.put(chas[i],++v); }else{ hash.put(chas[i],key); } } char rechar=0; for(char key2:hash.keySet()) { if(hash.get(key2)==1) { rechar=key2; //break; } } System.out.println(rechar); } public static void main(String[]args) { String str="abbbabcd"; getChar(str); } }