HashMap 存储在另 HashMap 并提高性能
问题说明
我应该在另一个 HashMap
中创建一个 HashMap
如下所示,它可以根据键将值存储在内部 HashMap
中运行时外层HashMap
I am supposed to created a HashMap
inside another HashMap
as shown below which can store the value inside the inner HashMap
based on the key of the outer HashMap
at the runtime
即程序所需的输出应该是格式
i.e. required output for program should be of the format
{ 1 = {11 = "aaa",15 = "bbb"}, 2 = {13 = "ccc", 14 = "ddd"} }
其中 1,2 是 Outer HashMap 的键值.
where 1,2 are Key values for Outer HashMap.
下面是为其提供的代码有没有更好的方法来提高性能
Below is the Code provided for it Is there any better approach to improve performance
HashMap<Integer, HashMap<Integer, String>>Outer
= new HashMap<Integer, HashMap<Integer,String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
for(int i =0;i<count;i )
{
String input[] = br.readLine().split("\s");
//HashMap<Integer,String>inner = new HashMap<Integer, String>();
int key = Integer.parseInt(input[0]);
if(Outer.isEmpty() || !Outer.containsKey(key))
{
HashMap<Integer, String> inner = new HashMap<Integer, String>();
inner.put(Integer.parseInt(input[1]),input[2]);
Outer.put(key, inner);
}
else if(Outer.containsKey(key))
{
HashMap<Integer, String> inner = (HashMap<Integer, String>) Outer.get(key).clone();
inner.put(Integer.parseInt(input[1]), input[2]);
Outer.put(key, inner);
}
}
正确答案
类似于 Vadim 的答案,但进一步改进 - 因为它不需要同时调用 containsKey
和 获取
:
Similar to Vadim's answer, but further improved - as it doesn't require a call to both containsKey
as well as get
:
Map<Integer, Map<Integer, String>> outer = new HashMap<Integer, Map<Integer, String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
Pattern splitter = Pattern.compile("\s");
for(int i = 0; i < count; i ){
String input[] = splitter.split(br.readLine());
int key = Integer.parseInt(input[0]);
Map<Integer, String> inner = outer.get(key);
if(inner == null){
inner = new HashMap<Integer, String>();
outer.put(key, inner);
}
inner.put(Integer.parseInt(input[1]), input[2]);
}
它还对命名约定以及使用 Collections 接口而不是具体类型进行了一些小的改进.
It also has some minor improvements for naming conventions, and use of the Collections interfaces instead of concrete types.
我还删除了对 clone
的调用.这可能会节省一点点 - 我认为它不会给您带来预期的结果.
I also removed the call to clone
. This could be a slight savings - and I don't think it would have given you your expected results.
最后 - 我更改的另一件事可能会略有改进,即使用预编译模式将字符串拆分为字段.
Finally - one other thing that I changed that could be a slight improvement is using a pre-compiled Pattern for the splitting of your String into fields.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicbag
-
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
复制文件夹/文件而不修改属性?
it1352 07-15 -
为什么字符'^'被Python Popen忽略 - 在Popen Windows转义'^'字符?
it1352 08-07