简体   繁体   English

将值分配给地图(哈希图或多图)

[英]Assigning values to Map (Hashmap or Multimap)

I have a map : 我有一张地图:

HashMap<String, ArrayList<Integer>> hashmap = new HashMap<String, ArrayList<Integer>>();

After retrieving data from a remote database server, I am creating a hashmap:(output in eclipse) 从远程数据库服务器检索数据后,我正在创建一个hashmap :(在eclipse中输出)

{newcomer=[23, 78, 118, 155, 240, 244], zoom=[213], profession=[8, 10, 12, 13, 15]}

Now, my concern is, it is taking too much of time to connect to server and download data. 现在,我担心的是,连接服务器和下载数据花费了太多时间。 Size of data is beyond limit of my patience. 数据大小超出了我的耐心范围。 Is there any way, I can store value directly in a Hashmap variable (like we do with normal variables, int i =5); 有什么办法,我可以将值直接存储在Hashmap变量中(就像我们对普通变量所做的那样,int i = 5);

In general, how can I do this (like I do in Python) : 通常,我该如何做(就像我在Python中一样):

hashmap = {newcomer=[23, 78, 118, 155, 240, 244], zoom=[213], profession=[8, 10, 12, 13, 15]}

I have switched to Guava recently, Can I do this Multimap as well? 我最近切换到了番石榴,我也可以做这个Multimap吗? Value I want to assign is now a string, so assigning might be much more complicated than I am thinking. 我现在想分配的值是一个字符串,因此分配可能比我想的要复杂得多。

Edit 1 : I need this for development purpose only. 编辑1 :我只需要出于开发目的。 Once application is live, will reconnect to database server. 应用程序上线后,将重新连接到数据库服务器。

Edit 2 : I have nearly 50k pairs at this moment, hence adding individually is not an option. 编辑2 :目前我有将近50k对,因此不能单独添加。 May, for development, I can trim list to 100 items, but that too involve labor to put each one. 可能,为了发展,我可以将清单缩小到100个项目,但是要逐个列出每个项目也需要人工。

Edit 3 : I understand we can figure out someway or other to setup database more perfectly for fast execution. 编辑3 :我知道我们可以通过某种方式找出更完美的数据库来快速执行。 I only need to figure out that is it possible to assign? 我只需要弄清楚是否可以分配? Else, I am going for local setup. 否则,我要进行本地设置。

Inline initialization of Map s is not possible in Java. Java无法进行Map的内联初始化。 In your case this doesn't matter however -- with the amout of data you have populating the Map via code either means hours (or days) of highly repetetive and extremely error-prone manual typing or writing a code generator: 但是,对于您而言,这并不重要-随着数据量的增加,您通过代码填充“地图”要么意味着数小时(或数天)的高度重复且极易出错的手动输入,要么编写了代码生成器:

I have nearly 50k pairs at this moment, hence adding individually is not an option. 我目前有将近5万对,因此不能单独添加。

And if you write a code generator anyway, it doesn't matter if it generates an inline initializer or loads of Map#put() statements. 而且,无论如何,如果您编写代码生成器,则生成一个内联初始化程序或加载Map#put()语句都没关系。

However, code generation / initialization in code certainly is a bad choice in this case anyway. 但是,无论如何在这种情况下,代码的生成/初始化当然是一个错误的选择。 Better solutions would be (sorted by decreasing usefulness): 更好的解决方案将是(按使用率递减排序):

  • Take care of your slow database setup or unoptimized query (see comment from CBass to the original question). 请注意您缓慢的数据库设置或未优化的查询(请参阅CBass对原始问题的评论)。
  • Take care to only load that data once, making the delay less relevant to the big picture. 注意只加载一次数据,使延迟与全局无关。
  • Read it from the DB server once, then serialize it to a file (see ObjectOutputStream ) and deserialize it from there afterwards. 从DB服务器读取一次,然后将其序列化为文件(请参见ObjectOutputStream ),然后从那里反序列化。
  • You could set up a local (fast) database server and read from that instead of the remote one. 您可以设置本地(快速)数据库服务器,然后从中读取而不是远程数据库服务器。

I wouldn't even think of initializing a data structure of this size in code unless all the above things have been shown to not work. 除非证明上述所有内容无效,否则我什至不会考虑在代码中初始化这种大小的数据结构。 Which is extremely unlikely. 这是极不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM