1。統計文字列文字の数(およびアルファベット順の順序を維持)
例:aabbbbbbbbba oh bcab cdabc deaaa
私が知っている5つの方法があります。より良いものがある場合は、アドバイスをください。
//方法1 public static void lettercount1(string s){s = s.replaceall( " +"、 ""); // 1、文字配列に変換char c [] = s.tochararray(); map <文字、integer> tree = new treemap <character、integer>(); for(int i = 0; i <c.length; i ++){// first:a、1 // second:a、2 // 2、キー整数値= tree.get(c [i]); // decompile:integer value =(integer)tree.get(charatele.valueof(c [i]))に対応する値を取得します。 // 3、判断ツリーを保存します。put(c [i]、value == null?1:値+1); } //結果形式が必要な場合:a(5)b(4)c(3)d(2)e(1)stringbuilder sbu = new StringBuilder(); for(文字key:tree.keyset()){integer count = tree.get(key); sbu.append(key).append( "(")。append(count).append( ")"); } // sbuをstring system.out.println(sbu.tostring());} // method 2 reack stream //これは、// nなどの特殊文字をテストするときに間違っています。これはMAPによって引き起こされます//ソリューションTreemap public static void lettercount2(string s){s = s.replaceall( " +"、 ""); treemap <string、long> result = arrays.stream(s.split( "")).sorted()// .collect(collectors.groupingby(function.identity()、collectors.counting())); .collect(collectors.groupingby(function.identity()、treemap :: new、collectors.counting())); system.out.println(result); } //方法3 collections.frequency //実際には、文字列をコレクションに変換して各文字列を保存し、各文字列をコレクションと比較します。 List <String> list = arrays.aslist(s.split( "")); map <string、integer> map = new treemap <string、integer>(); for(string str:list){map.put(str、collections.frequency(list、str));} system.out.println(map); } //方法4 public static void lettercount4(string s){s = s.replaceall( " +"、 ""); string [] strs = s.split( ""); map <string、integer> map = new treemap <string、integer>(); for(string str:strs){map.put(str、stringcount(s、str));} system.out.println(map); } //方法5 public static void lettercount5(string s){s = s.replaceall( " +"、 ""); string [] strs = s.split( ""); map <string、integer> map = new treemap <string、integer>(); for(string str:strs){map.put(str、stringcount2(s、str));} system.out.println(map); } //分割static static int stringcount(string maxstr、string substr){// note // 1。たとえば、qqqqが見つからない場合、文字列は直接返されます// 2。たとえば、qqjava、他の文字は最後にありません。自動的に入力// 4。特殊文字の場合は、エスケープ文字int count =(maxstr + "").split(subst).length -1; // system.out.println( "/" " + minstr +"/"" + "文字列の数:" + count); return count;} // stringcount2(string maxstr、string subsstr){int count = 0; matcher m = pattern.compile(substr).matcher(maxstr); while(m.find()){count ++;} return count;}2。統計文字列内の単語数(英語のみ)
これは実際には上記と同じであり、以下に簡単な方法のみを記述します
public static void wordstringcount(string s){//開始は文字列であり、分割した後、文字列ストリームになります。 map <string、long> result = arrays.stream(s.split( "// s+")).map(word-> word.replaceall( "[^a-za-z]"、 "")).collect(collectors.identity()、collectors.counting())); system.out.println(result); }3。統計テキストワードの数(英語のみ)
//テキストで単語の数を検索しますpublic static void wordfilecount(string path)throws ioexception {// string streamは最初に最初に// firrst // firrst // first first // first // filter map <string、long> result = files.lines(paths.get(path)、path)、charset.defaultharset().paralle stream.flatmap(str-> arrays.stream(str.split( " +"))).map(word-> word.replaceall( "[^a-za-z]"、 ""))// empty.filter(word-> word.length()> 0).collect(collectors.identity(); collectors.identity()、collectors.identity()、collectors buntidity(); system.out.println(result); }4。その他の無関係
パラメーターを渡すことなく、変数パラメーターリストを渡すことができることがわかっています。
のために
public void testname(){system.out.println( "a"); } public void testname(string ... s){//パラメーターは渡されません。sは、デフォルトでsystem.out.println( "b"); }testname()を呼び出すときに何を印刷すればよいですか?それはaを印刷し、パラメーターが本当に空になっている方法と自動的に一致します
Java8が文字列文字の数をカウントするためのいくつかの方法の上記の概要(推奨)は、エディターが共有するコンテンツ全体です。私はそれがあなたに参照を与えることができることを願っています、そしてあなたがwulin.comをもっとサポートできることを願っています。