簡體   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