Java实现Map <string,string =“”>类和使用iterator </string,>

时间:2012-03-31 18:38:24

标签: java interface iterator

任何人都可以向我解释实现Map类的意义,我应该如何创建迭代器?我在过去一小时内用谷歌搜索了这个,我真的不明白应该如何实现这样的界面。

提前感谢您提供相关信息。

2 个答案:

答案 0 :(得分:2)

您可能感兴趣的一个网站 - http://www.sergiy.ca/how-to-iterate-over-a-map-in-java/以及手边的示例:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<Integer, Integer> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

答案 1 :(得分:0)

是的,它有点难以掌握,但请仔细研究这些:

实施简单的地图条目

public class GenericEntry<KeyType , ValueType> {
    private final KeyType key;
    private ValueType value;

    public MyEntry(KeyType key, ValueType value) {
        this.key = key;
        this.value = value;
    }

    public KeyType getKey() {
        return key;
    }

    public ValueType getValue() {
        return value;
    }

    public void setValue(ValueType value) {
        this.value = value;
    }
}

来源:How to implement Map(and other data struct.) in plain Java

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Map.html

相关问题