http://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character
I came up with this: http://ideone.com/edaq3X
(Note that I haven't used Java for a long time, and I hate it's guts...this exercise reminded me just how much it sucks)
You don't need to initialize a hashmap, but in this case it doesn't really matter.
Edit: Here is a version that doesn't initialize the hashmap: http://ideone.com/k88VPa
Note that this time the hashmap will not contain keys with a value of 0. You would need to account for this when accessing the hashmap, or insert the keys explicitly.
I came up with this: http://ideone.com/edaq3X
(Note that I haven't used Java for a long time, and I hate it's guts...this exercise reminded me just how much it sucks)
Code:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Map hitcounts = new HashMap();
String uppercase_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (char uppercase_char : uppercase_alphabet.toCharArray())
{
hitcounts.put(uppercase_char, 0);
}
String input_string = "Make This WhatEveR input You Want";
for (char character : input_string.toCharArray())
{
if (hitcounts.containsKey(character))
{
hitcounts.put(character, (int)hitcounts.get(character) + 1);
}
}
System.out.println(hitcounts);
}
}You don't need to initialize a hashmap, but in this case it doesn't really matter.
Edit: Here is a version that doesn't initialize the hashmap: http://ideone.com/k88VPa
Code:
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Map hitcounts = new HashMap();
String input_string = "Make This WhatEveR input You Want";
for (char character : input_string.toCharArray())
{
if (Character.isUpperCase(character))
{
int hitcount = 0;
if (hitcounts.containsKey(character))
{
hitcount = (int)hitcounts.get(character);
}
hitcounts.put(character, hitcount + 1);
}
}
System.out.println(hitcounts);
}
}Note that this time the hashmap will not contain keys with a value of 0. You would need to account for this when accessing the hashmap, or insert the keys explicitly.
