简体   繁体   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?

For a project, I have to create a map where the keys are List s of String and the value is two integers. 对于一个项目,我必须创建一个映射,其中的键是String List ,值是两个整数。 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-each循环:

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. 不,这里的嵌套泛型(通常)不是问题。

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

相关问题 LinkedList无法转换为布尔值,我在做什么? - LinkedList can not be converted to boolean, what am i doing wronge? 我正在用Java中的“ 0”替换Evens,我在做什么错? - I am trying to replaceEvens with a “0” in java, what am I doing wrong? 我正在尝试创建一个循环的LinkedList,请告诉我它是否是正确的方法 - I am trying to create a circular LinkedList Please tell me if its a correct way to do it 尝试从文件中读取数据后创建HashMap。 获取空值。 我究竟做错了什么? - Trying to create a HashMap after reading data from file. Getting null values. What am I doing wrong? 我做错了什么 - What am i doing wrong with this 我究竟做错了什么?? - What am I doing wrong?? 试图让我的弦向左旋转。 我在这里做错了什么? - Trying to get my string to rotate left. What am I doing wrong here? 尝试将字符串转换为MD5时我做错了什么 - What am I doing wrong when trying to convert string to MD5 试图在Java中测试矩形的交集,我做错了什么? - Trying to test rectangles for intersection in Java, what am I doing wrong? 基本的银行程序将无限期地执行do循环。 我究竟做错了什么? - Rudimentary bank program is going through the do loop infinately. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM