将HashMap存储在另一个HashMap中并提高性能 [英] Storing a HashMap inside another HashMap and improving performance

查看:116
本文介绍了将HashMap存储在另一个HashMap中并提高性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在另一个 HashMap 内创建一个 HashMap ,如下所示,它可以将值存储在内部基于外部 HashMap 在运行时的键 HashMap

即所需的程序输出应该是格式

  {1 = {11 =aaa,15 =bbb}, 2 = {13 =ccc,14 =ddd}} 

外部HashMap的关键值。



以下是提供的代码是否有更好的方法来提高性能

  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 以及 get

 映射< 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]);

映射< 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接口而不是具体的类型。



我也删除了对 clone 的调用。这可能是一个小的节省 - 我不认为它会给你预期的结果。



最后 - 我改变的另一件事可能是轻微的改进是使用预编译模式将字符串分割为字段。


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"} }

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

解决方案

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]);
}

It also has some minor improvements for naming conventions, and use of the Collections interfaces instead of concrete types.

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.

这篇关于将HashMap存储在另一个HashMap中并提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆