久久久久久久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屋-程序員
主站蜘蛛池模板: 成人深夜视频 | 日本黄色片视频 | 国产精品第一 | 一级片中文字幕 | 人人草人人草 | 欧美国产日韩在线 | 99re视频在线 | 91av免费 | 亚洲免费视频一区 | 五月婷综合 | 中文字幕二区 | 三年中文在线观看免费大全中国 | 欧美日韩亚洲国产 | 国产黄在线观看 | 亚洲成人av在线播放 | 福利网站在线观看 | 天堂网久久 | 久久综合社区 | 色香蕉网| 欧美精品99久久久 | www国产精品| 97在线超碰 | 亚洲黄色网址 | 欧美日韩免费 | 日本一级一片免费视频 | 国产午夜影院 | 中文在线观看视频 | 国产传媒av| 亚洲一区在线视频 | 青青草国产成人av片免费 | 精品久久国产 | 免费观看av | 欧美激情国产精品 | 国产一区欧美 | 免费日韩视频 | 成人免费看片在线观看 | 精品福利在线观看 | 日韩亚洲在线 | 欧美久久久久久久 | 日韩亚洲欧美在线 | 国产成人网 |