繁体   English   中英

我正在尝试遍历Hashmap <LinkedList<String> ,int [] &gt;&gt;。 有比我正在做的更简单的方法吗?

[英]I am trying to iterate through a Hashmap<LinkedList<String>, int[]>>. Is there a simpler way to do it than what I am doing?

对于一个项目,我必须创建一个映射,其中的键是String List ,值是两个整数。 因此,我做到了:

private Map<LinkedList<String>, int[]> playerProfile;
private List<String> previousChoices;

然后,稍后我必须遍历该映射并将所有键值组合写入数据文件。 所以我要建立一个这样的迭代器:

    Set<Entry<LinkedList<String>, int[]>> profileSet;
    profileSet = playerProfile.entrySet();

    //iterate through the Set
    List<String> curList; //current list of choices
    int[] curHeadTail; //current list of heads/tails
    Entry<LinkedList<String>, int[]> curEntry;
    Iterator<Entry<LinkedList<String>, int[]>> i =
    profileSet.iterator();

我想知道的是:是否有一种更简单的方法可以减少代码行? 在某一点上,我有三重嵌套的泛型。 太多了吗

当然,您可以使用for-each循环:

for(Entry<LinkedList<String>, int[]> curEntry : playerProfile.entrySet()){
    // now you can use curEntry
}

不,这里的嵌套泛型(通常)不是问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM