java中寻找字符串中重复的字符(转)
1 import java.util.HashMap; 2 import java.util.Map; 3 import java.util.Set; 4 5 public class findMulString { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 printDuplicateCharacters("adsfadsfadsfhhh"); 13 } 14 15 public static void printDuplicateCharacters(String word) 16 { 17 char[] charaters=word.toCharArray(); 18 Map<Character,Integer> charMap=new HashMap<Character,Integer>(); 19 for (Character ch: charaters) { 20 if(charMap.containsKey(ch)){ 21 charMap.put(ch, charMap.get(ch)+1); 22 }else{ 23 charMap.put(ch, 1); 24 } 25 } 26 Set<Map.Entry<Character,Integer>> entrySet=charMap.entrySet(); 27 System.out.printf("List of duplicate characters in Strng '%s' %n",word); 28 for(Map.Entry<Character, Integer> entry:entrySet){ 29 if(entry.getValue()>1){ 30 System.out.printf("%s:%d:%n",entry.getKey(),entry.getValue()); 31 } 32 } 33 } 34 } 35