我试图遍历Hashmap <LinkedList <String>,int [] >>.有没有比我正在做的更简单的方法呢?

Aar*_*own 0 java generics

对于一个项目,我必须创建一个键是Lists的映射,String值是两个整数.所以,我这样做了:

private Map<LinkedList<String>, int[]> playerProfile;
private List<String> previousChoices;
Run Code Online (Sandbox Code Playgroud)

然后我必须遍历地图并将所有键值组合写入数据文件.所以我正在设置一个像这样的迭代器:

    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();
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*iot 5

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

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

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