-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCharsInAString.java
More file actions
32 lines (24 loc) · 923 Bytes
/
Copy pathCountCharsInAString.java
File metadata and controls
32 lines (24 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package mapsLabdaStreamAPI;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class CountCharsInAString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputLine = scanner.nextLine();
char[] charArray = inputLine.toCharArray();
LinkedHashMap<Character,Integer> symbolsMap = new LinkedHashMap<>();
for (int i = 0; i < charArray.length ; i++) {
char symbol = charArray[i];
if(symbol == ' '){
continue;
}
if(!symbolsMap.containsKey(symbol)){
symbolsMap.put(symbol,1);
}else{
int currentValue = symbolsMap.get(symbol);
symbolsMap.put(symbol,currentValue + 1);
}
}
symbolsMap.entrySet().forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));
}
}