Map.Entry重定义,用于2维映射的迭代器 [英] Map.Entry redefinition, for iterator of 2 Dimensional Map
问题描述
我们有一个功课来实现一个类,它创建一个将成为字符串二维图的对象。 centralMap = new HashMap< String,Map< String,String>>
。教授给了我们一个接口,其中包含我们应该重新定义的方法,比如put方法( public String put(final String row,final String column,final String value)
)get方法( public String get(final String row,final String column)
)和其他一些方法..以及我无法重新定义的方法,是迭代器方法..在他给出的界面中,有一个类Entry,他说,我们只将其用于迭代器方法!但我不知道我们应该怎么做..这是类Entry,以及我们应该重新定义(实现)的迭代器方法:
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());
}
}
以及我们应该使用的迭代器方法重新定义是这个: @Override Iterator< Entry> iterator();
我该怎么办?我听说我们应该为迭代器实现一个新类..
告诉我你是否需要我实现的类,(并实现他给出的接口)知道我如何将嵌套映射放在另一个一个等等..因为嵌套的地图刚刚在put方法中创建..在我的构造函数中只有centralMap。
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!!
推荐答案
你只需要必须创建一个方法,创建一个 Iterator
条目。由于使用了地图,每个条目
都是唯一的。所以你只需要执行以下操作:
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
}
这篇关于Map.Entry重定义,用于2维映射的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!