[英]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.