久久久久久久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屋-程序員
主站蜘蛛池模板: 欧美一区二区三区免费电影 | 91免费观看| 欧美日韩在线成人 | 国产美女在线播放 | 国产欧美精品一区二区三区 | 精品国产欧美一区二区三区成人 | 在线一区观看 | 一级二级三级黄色 | 精品成人av | 天天干狠狠操 | 国产亚洲精品成人av久久ww | 日韩色综合 | sese视频在线观看 | 91av在线视频观看 | 免费观看一级特黄欧美大片 | 日韩欧美中文字幕在线观看 | 91国内精品久久 | 一级黄色毛片a | 欧美成人精品在线 | 日韩欧美国产一区二区 | 免费的色网站 | 国产精品日韩欧美一区二区三区 | 精品欧美乱码久久久久久 | 欧美一二区 | 亚洲综合久久久 | 国产欧美精品一区二区三区 | 91视频一区 | 一级片av| 久热久热 | 国产福利免费视频 | 在线成人av | 久久久久九九九女人毛片 | 亚洲综合一区二区三区 | 在线观看国产精品一区二区 | 91精品国产91综合久久蜜臀 | 一区二区三区视频在线免费观看 | 国产伦精品一区二区三区视频金莲 | 日韩成人性视频 | 国产精品一区久久久 | 国产精品自产av一区二区三区 | 久久国产欧美日韩精品 |