Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CharCheck;
- import java.util.HashMap;
- import java.util.Map;
- public class CheckChar {
- Map<Character, Integer> charMap = new HashMap<Character, Integer>();
- int uniqueCount = 0;
- public static void main(String[] args) {
- String stringToCheck = "checking to char array method";
- CheckChar collector = new CheckChar();
- collector.charMap("checking to char array method");
- collector.showCharSum();
- collector.showUnique();
- }
- private void showCharSum() {
- System.out.println("Chars entry count");
- charMap.forEach((k, v) ->
- System.out.println("Key: " + k + " -> Value: " + v)
- );
- }
- private void showUnique() {
- System.out.println("Which char is unique: ");
- charMap.forEach((k, v) -> {
- if (v < 2) {
- System.out.println("Key: " + k + " -> Unique");
- uniqueCount++;
- }
- });
- System.out.println("How many unique chars: " + uniqueCount);
- }
- private void charMap(String stringToCheck){
- System.out.println();
- char[] charArray = stringToCheck.toCharArray();
- for (char temp : charArray) {
- if (charMap.get(temp) != null) {
- int nVal = charMap.get(temp) + 1;
- charMap.put(temp, nVal);
- System.out.println("add to " + temp + " +1");
- } else {
- charMap.put(temp, 1);
- System.out.println("add new " + temp + " & val 1");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment