1. 통계 문자형 문자 수 (및 알파벳 순서를 유지)
예 : AABBBBBBBA OH BCAB CDABC DEAAA
내가 아는 5 가지 방법이 있습니다. 더 나은 것이 있다면 조언을 해주세요.
// 메소드 1 public static void lettercount1 (문자열 s) {s = s.replaceall ( " +", ""); // 1, 문자 배열로 변환 char c [] = s.tochararray (); 지도 <문자, 정수> tree = new treemap <문자, integer> (); for (int i = 0; i <c.length; i ++) {// 첫 번째 : a, 1 // 두 번째 시간 : a, 2 // 2, 키 정수 값 = tree.get (c [i]); // decompile : (정수 값 = (integer) tree.get (c [i])에 해당하는 값을 가져옵니다. // 3, 판단 트리를 저장합니다. } // 결과 형식이 필요한 경우 : A (5) B (4) C (3) D (2) E (1) StringBuilder SBU = New StringBuilder (); for (문자 키 : tree.keyset ()) {integer count = tree.get (key); sbu.append (key) .append ( "("( ")). Append (count) .append (") "); } // sbu를 string system.out.println으로 변환합니다 (sbu.tostring ());} // 메소드 2 스트림을 사용하십시오. 이것은 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 (결과); } // 메소드 3 Collections.Frequency // 실제로 문자열을 컬렉션으로 바꾸고 각 문자열을 저장하고 각 문자열과 컬렉션과 비교하는 것입니다. public static void LetterCount3 (string s) {s = s.replaceall ( " +", ""); list <string> list = arrays.aslist (s.split ( "")); 지도 <문자열, 정수>지도 = 새로운 treemap <문자열, 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 ( " +", ""); 문자열 [] strs = s.split ( ""); 지도 <문자열, 정수>지도 = 새로운 treemap <문자열, 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 ( " +", ""); 문자열 [] strs = s.split ( ""); 지도 <문자열, 정수>지도 = 새로운 treemap <문자열, integer> (); for (string str : strs) {map.put (str, StringCount2 (s, str));} system.out.println (map); } // 분할 공개 static int stringCount (String maxstr, string substr) {// note // 1. 예를 들어, QQQQQ를 찾지 못하면 String이 직접 반환됩니다. 자동으로 // 4. 특수 문자의 경우 Escape int count = (maxstr + "") .split (substr) .length -1; // system.out.println ( "/" "" + minstr + "/" " +"문자열 발생 수 : " + count;} // case -insensitive, compile (minstr, _insendestive)를 원한다면 excipational int count = (maxstr +" ") .split (split (substr) .length -1; // system.out.println (system.println); StringCount2 (String maxstr, String substr) {int count = 0; matcher m = pattern.compile (substr) .matcher (maxstr); while (m.find ()) {count ++;} return count;}2. 통계 문자열의 단어 수 (영어 만 해당)
이것은 실제로 위와 동일하며 간단한 방법 만 아래에 기록됩니다.
public static void wordstringCount (문자열 s) {// 시작은 문자열이며, 분할 후 문자열 스트림이됩니다. map <string, long> result = arrays.stream (s.split ( "// s+")). System.out.println (결과); }3. 통계 텍스트 단어 수 (영어 만 해당)
// 텍스트에서 단어 수를 검색 공개 정적 void WordFileCount (String Path)는 ioException {// 문자열 스트림이 처음에 첫 번째입니다. // 첫 번째 // 첫 번째 // 문자 스트림으로 분할 // filter map <string, long> result = files.lines (paths.get (path), charset.defaultcheset (). stream.flatmap (str-> arrays.stream (str.split ( " +"))) .map (word-> word.replaceall ( "[^a-za-z]" "," ") // empty.filter를 제거합니다 (word-> word.length ()> 0). System.out.println (결과); }4. 다른 무의미한
변수 매개 변수 목록은 매개 변수를 전달하지 않고 전달 될 수 있음을 알고 있습니다.
~을 위한
public void testName () {system.out.println ( "a"); } public void testName (string ... s) {// 매개 변수가 전달되지 않으며 s는 기본적으로 시스템을 초기화합니다. }TestName ()를 호출 할 때 무엇을 인쇄해야합니까? 그것은 a를 인쇄하고 매개 변수가 실제로 비어있는 메소드와 자동으로 일치합니다.
Java8이 문자열 문자 수 (권장) 수를 계산하는 몇 가지 방법에 대한 위의 요약은 편집기가 공유하는 전체 내용입니다. 나는 그것이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.