Map.Entry redefinition, for iterator of 2 Dimensio

2019-09-08 05:38发布

问题:

We have a homework, to implement a class, that creates an Object that will be a 2Dimensional Map of Strings. centralMap = new HashMap<String, Map<String,String>>. The professor gave us an interface, that contains the methods that we should redefine, like the put method (public String put(final String row, final String column, final String value)) the get method (public String get(final String row, final String column)) and some other methods.. and the one that i couldn't redefine, is the iterator method.. In the interface that he gave, there was a class Entry, that he said, we ll use it just for the iterator method! But I have no idea what should we do with it.. Here is the class Entry, and the iterator method that we should redefine(implement):

final class Entry
{
    /** First Key. */
    private final String key1;

    /** Second Key. */
    private final String key2;

    /** Value. */
    private final String value;

    /** Cponstructor for a new Tripel.
     * @param key1 First Key.
     * @param key2 Second Key.
     * @param value Value.
     */
    public Entry(final String key1, final String key2, final String value)
    {
        this.key1 = key1;
        this.key2 = key2;
        this.value = value;
    }

    public String getFirstKey()
    {
        return key1;
    }

    public String getSecondKey()
    {
        return key2;
    }

    public String getValue()
    {
        return value;
    }

    @Override public boolean equals(final Object anything)
    {
        if(anything == null)
            return false;
        if(getClass() != anything.getClass())
            return false;
        final Entry that = (Entry)anything;
        return Objects.equals(getFirstKey(), that.getFirstKey())
               && Objects.equals(getSecondKey(), that.getSecondKey())
               && Objects.equals(getValue(), that.getValue());
    }

    // CHECKSTYLE- Magic Number
    @Override public int hashCode()
    {
        int hash = 7;
        hash = 17 * hash + Objects.hashCode(getFirstKey());
        hash = 17 * hash + Objects.hashCode(getSecondKey());
        hash = 17 * hash + Objects.hashCode(getValue());
        return hash;
    }
    // CHECKSTYLE+ Magic Number

    @Override public String toString()
    {
        return String.format("(%s, %s, %s)", getFirstKey(), getSecondKey(), getValue());
    }

}

and the iterator method that we should redefine is this one: @Override Iterator<Entry> iterator(); How should I proceed? I heard that we should implement a new class just for the iterator.. tell me if you need the class that I implemented, (and which implements the interface he gave) to know how i put the nested map in the other one etc.. because the nested map is just created in the put method.. in my constructor there s just the centralMap.

Thanks a lot for your help!!

回答1:

You simply have to create an method, that creates an Iterator of entries. Every Entry is unique, because of the used Maps. So you just need to do something like:

for entryMap in map do {
  entryMap get list of keysAndValues
  for keyValue in keysAndValues preppend this key
}