Java implementation counts the number of string occurrences
- 2021-11-30 00:06:08
- OfStack
This article example for everyone to share Java to achieve statistical string occurrence of the specific code, for your reference, the specific content is as follows
Requirements:
A string is entered on the key disk, and the number of times each string appears in the string is required to be counted.
Example: Keyboard entry "aababcabcdabcde"
Output on the console: "a (1) b (4) c (3) d (2) e (1)"
Thoughts:
① Enter a string on the key disk
Create the HashMap set with the key Character and the value Integer
Thirdly, traverse the character application to get every 1 character
4. Take every 1 character as a key to find the corresponding value in HashMap set and see its return value
If the return value is null: indicating that the character does not exist in the HashMap collection, store the character as the key and 1 as the value
If the return value is not null: This means that the character exists in the HashMap collection, add 1 to the value, and then store the character and the paired value again
⑤ Traverse the HashMap set to get keys and values, and splice them according to requirements
⑥ Output result
public class StrCount {
public static void main(String[] args) {
// Keyboard entry 1 String
Scanner sc = new Scanner(System.in);
System.out.println(" Clear input 1 String string :");
String line = sc.nextLine();
// Create HashMap Set , The key is Character, Value is Integer
HashMap<Character, Integer> map = new HashMap<>();
// Traverse the character application and get every 1 Characters
for (int i = 0; i < line.length(); i++) {
char key = line.charAt(i);
// Every thing you get 1 Characters as keys to HashMap Collection to find the corresponding value and see its return value
Integer value = map.get(key);
if (value == null) {
// If the return value is null: Indicates that the character is in the HashMap Does not exist in the collection , You use that character as the key, 1 Store as a value
map.put(key, 1);
} else {
// If the return value is not ull: Indicates that the character is in the HashMap Existence in the collection , Add the value 1, Then re-store the character and the paired value
value++;
map.put(key, value);
}
}
// Traversal HashMap Set , Get keys and values, and splice them as required
StringBuilder sb = new StringBuilder();
Set<Character> keySet = map.keySet();
for (Character key : keySet) {
Integer value = map.get(key);
sb.append(key).append("(").append(value).append(")");
}
// Output result
String result = sb.toString();
System.out.println(result);
}
}
The last traversal output was written with the teacher, but the following one I wrote was not posted because I thought the teacher's one was more in line with the meaning of the question, using splicing, and the results were all the same.
// Traversal HashMap Set , Get keys and values, and splice them as required
Set<Character> keySet = map.keySet();
for (Character key : keySet) {
Integer value = map.get(key);
// Output result
System.out.print(key + "(" + value + ")");
}