久久久久久久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屋-程序員
主站蜘蛛池模板: 欧美综合一区二区 | 女同久久另类99精品国产 | 另类亚洲视频 | 午夜影院操 | 国产农村一级片 | 欧洲一区二区在线 | 中文字幕欧美一区 | 日韩av在线一区二区 | 欧美极品在线视频 | 欧美区在线| 亚洲精品二区 | 成年人黄色免费视频 | 午夜影视大全 | 91九色在线观看 | 欧美一区二区三区在线看 | 国产精品免费一区二区三区四区 | 国产成人免费视频网站高清观看视频 | 久久精品一区 | 日韩av一区二区在线观看 | 在线播放精品视频 | 在线中文字幕av | 欧美 日韩 国产 在线 | 国产亚洲网站 | av手机在线| 在线一区二区三区 | 色婷婷国产精品综合在线观看 | 91精品国产一区二区三区 | 在线视频一区二区三区 | 久久久精品天堂 | 91精品国产91久久久久游泳池 | 精品毛片 | 色偷偷噜噜噜亚洲男人 | 国产区一区 | 成人影院一区二区三区 | 亚洲欧美视频 | 999精品视频 | www日本高清视频 | 日韩精品一区二区三区免费视频 | 亚洲天堂免费在线 | 欧美视频精品 | 男人久久天堂 |