I am extending a HashMap
to implement a class that documents the solution steps for an equation:
public class SolutionSteps extends HashMap<Integer, String>
{
private int currentStep;
public SolutionSteps ()
{
super();
currentStep = 1;
}
public final void addStep (@NotNull final String value)
{
put(currentStep, value);
currentStep++;
}
@Override
@NotNull
public final String toString ()
{
StringBuilder sb = new StringBuilder(50 * size());
for(Entry<Integer, String> entry : entrySet())
{
sb.append(entry.getKey()).append(": " ).append(entry.getValue()).append('\n');
}
return sb.toString().trim();
}
}
So far, I have not had a problem with the order of these entries when testing - I will add test entries, and the toString()
will print them out in the correct ordering and numbering, i.e.
SolutionSteps steps = new SolutionSteps();
steps.addStep("First");
steps.addStep("Second");
steps.addStep("Third");
steps.addStep("Fourth");
steps.addStep("Fifth");
Will produce the output in the intended order:
1: First
2: Second
3: Third
4: Fourth
5: Fifth
My question is, is this ordering of the entry set guaranteed?