久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 HashMap 存儲在另一個 HashMap 中并提高性能

Storing a HashMap inside another HashMap and improving performance(將 HashMap 存儲在另一個 HashMap 中并提高性能)
本文介紹了將 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.

這篇關于將 HashMap 存儲在另一個 HashMap 中并提高性能的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 蜜桃在线一区二区三区 | 欧美1级| 中文字幕1区2区 | 欧美国产视频 | 日韩二三区| 精品欧美乱码久久久久久1区2区 | av国产在线观看 | 久精品久久| 成人av资源在线 | 在线观看三级av | 天天插天天操 | 日韩欧美成人精品 | 久久一级大片 | av毛片在线播放 | 久久久久国产精品 | av永久 | 中文字幕99 | 国产一区二区视频免费在线观看 | 久久91精品久久久久久9鸭 | 精品国产免费人成在线观看 | 亚洲成色777777在线观看影院 | www.色综合 | 国产成人在线一区二区 | 日韩美女在线看免费观看 | 日韩一区和二区 | 亚洲精品乱码8久久久久久日本 | 国产精产国品一二三产区视频 | 亚洲高清视频在线 | 国产片侵犯亲女视频播放 | 超碰成人免费 | 精品国产免费人成在线观看 | 极品一区| 国家aaa的一级看片 h片在线看 | 久久精品—区二区三区 | 99国产精品99久久久久久 | 久久尤物免费一区二区三区 | 亚洲色图婷婷 | 伊人艹 | 国产精品欧美一区二区三区 | 欧美色视频免费 | 久久精品成人热国产成 |