Guest User

Untitled

a guest
Apr 26th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. package CharCheck;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class CheckChar {
  7. Map<Character, Integer> charMap = new HashMap<Character, Integer>();
  8. int uniqueCount = 0;
  9.  
  10. public static void main(String[] args) {
  11. String stringToCheck = "checking to char array method";
  12. CheckChar collector = new CheckChar();
  13. collector.charMap("checking to char array method");
  14. collector.showCharSum();
  15. collector.showUnique();
  16. }
  17.  
  18. private void showCharSum() {
  19. System.out.println("Chars entry count");
  20. charMap.forEach((k, v) ->
  21. System.out.println("Key: " + k + " -> Value: " + v)
  22. );
  23. }
  24.  
  25. private void showUnique() {
  26. System.out.println("Which char is unique: ");
  27. charMap.forEach((k, v) -> {
  28. if (v < 2) {
  29. System.out.println("Key: " + k + " -> Unique");
  30. uniqueCount++;
  31. }
  32. });
  33. System.out.println("How many unique chars: " + uniqueCount);
  34. }
  35.  
  36. private void charMap(String stringToCheck){
  37. System.out.println();
  38. char[] charArray = stringToCheck.toCharArray();
  39. for (char temp : charArray) {
  40. if (charMap.get(temp) != null) {
  41. int nVal = charMap.get(temp) + 1;
  42. charMap.put(temp, nVal);
  43. System.out.println("add to " + temp + " +1");
  44. } else {
  45. charMap.put(temp, 1);
  46. System.out.println("add new " + temp + " & val 1");
  47. }
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment