简体   繁体   中英

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

For a project, I have to create a map where the keys are List s of String and the value is two integers. So, I made it like this:

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

Then later I have to iterate through the map and write all the key-value combinations to a data file. So I am setting up an iterator like this:

    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();

What I want to know is: is there a simpler way to do this that takes less lines of code? And at one point I have triply-nested generics. Is that too much?

Sure, you can use a for-each loop:

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

And no, nested generics here (and in general) are not a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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