使用循环从 HashSet 值创建 HashMap
我正在尝试从 HashSet 的值创建 HashMap (它本身作为值存储在 HashMap 中)。
我不确定是否要迭代或使用 for 循环。
示例:
newMap = new HashMap<String, HashSet<String>>();
HashSet<String> boxing = new HashSet<String>();
newMap.put("fighting", boxing);
boxing.add("jab");
boxing.add("hook");
boxing.add("uppercut");
现在我需要迭代或循环,以便“装箱”HashSet 中的每个值创建一个新的 HashMap,并将该值作为新映射的键。
因此 newMap1 将以 'jab' 作为键,newMap2 将以 'hook' 作为键,依此类推。
任何帮助表示赞赏。
I'm trying to create a HashMap from values of a HashSet (which itself is stored as values in a HashMap).
I'm not sure whether to Iterate or use a for loop.
Example:
newMap = new HashMap<String, HashSet<String>>();
HashSet<String> boxing = new HashSet<String>();
newMap.put("fighting", boxing);
boxing.add("jab");
boxing.add("hook");
boxing.add("uppercut");
Now I need to iterate or loop through so that each value in the 'boxing' HashSet creates a new HashMap with the value as the key for the new map.
So newMap1 would have 'jab' as the key, newMap2 would have 'hook' as the key and so on.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Java 9 开始你可以这样做:
Since Java 9 you can do this: