方法一:用集合map
package com.company; import java.util.*; /** * Created by json on 2017/10/2. */ public class Collection { public static void main(String[] args) { Map<Character ,Integer>maps=new LinkedHashMap<>(); Scanner in=new Scanner(System.in); while(in.hasNext()){ String str=in.next(); for (int i = 0; i < str.length(); i++) { char key=str.charAt(i); if(!maps.containsKey(key)) maps.put(key,1); else{ int val=maps.get(key); maps.put(key,val+1); } } int max=0; for (Character character : maps.keySet()) { if(maps.get(character)>max) max=maps.get(character); } for (Character character : maps.keySet()) { if(maps.containsValue(1)&&maps.get(character)==1){ System.out.println("第一个出现一次的字符" + character); break; }else if(maps.get(character)==max){ System.out.println(character+" "+max); break; } } maps.clear(); } } }