Javaインプリメンテーション:文字列に重複しない最初の文字を見つけます.

681 ワード

public static char getFirstNoRepeatChar(String str) { 

        //   LinkedHashMap            

       Map map = new LinkedHashMap( 

               str.length()); 

        for (char c :str.toCharArray()) { 

            //      k          

            map.put(c,map.containsKey(c) ? map.get(c) + 1 : 1); 

        } 

        //   Map        1        

        for(Entry entry : map.entrySet()) { 

            if(entry.getValue() == 1) 

                returnentry.getKey(); 

        } 

        throw newRuntimeException("         "); 

    }