HashMap counter?
in
Programming Questions
•
1 year ago
I'm using HashMaps for the first time by looking at reference:
The reason I'm using one is to make a n-order Markov Chain. The first thing I need to do is make a list / map (what is the right term?) of what characters are used from an input file and keep track of how many times they appear. I have a short code that seems to do this, I guess my question is if this is how HashMaps are supposed to be used. I saw Daniel Shiffman's example on HashMaps uses a Class for something similar:
My code (the one I'm not sure about usage wise) is below:
int markovOrder = 1;
HashMap baseCharacters = new HashMap();
void setup() {
size(100, 100);
String content = join(loadStrings("inputFile.txt"), " ");
if (content.length() > 0) {
buildReference(content);
}
else println("No content!");
}
void buildReference(String fromFile) {
for (int i = 0; i < fromFile.length()-markovOrder; i++) {
String builder = fromFile.substring(i, i+markovOrder);
if (baseCharacters.containsKey(builder)) {
int counter = baseCharacters.get(builder).hashCode();
baseCharacters.put(builder, counter+1);
}
else baseCharacters.put(builder, 1);
}
println(baseCharacters);
}
1