GeeCON Prague 2014*
The moose likes Java in General and the fly likes Optimising size of HashMap in runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Optimising size of HashMap in runtime

Madhan B Babu
Greenhorn

Joined: Nov 16, 2010
Posts: 5
Hi

I have a use case where i am pushing tens of thousands of data into a HashMap.

the structure of the HashMap is like
HashMap<String1, Map<String2, Map<String3, String4>>>

Here the String1 would anyway be unique, but the String3 and String 4 are very frequently repeating Strings.
These are representing the Status and Priority values in each "calendar week" (String 2)

The String3 (Status) and String4 (Priority) values can only be withing a predefined set of ten to 15 strings.

Now when i use the below code for serializing the hash map,
----------------------------------------------------------------------------------------------------------------
File file=new File("C:\\testData1.ser");
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(new DeflaterOutputStream(fos));
oos.writeObject(MasterChartingData);
oos.flush();
oos.close();
fos.close();

----------------------------------------------------------------------------------------------------------------
and the below code for deserializing,
----------------------------------------------------------------------------------------------------------------
File file=new File("C:\\testData1.ser");
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(fis));
HashMap DeserializedMasterChartingData=(HashMap<String, Map<String,Map<String,String>>>)ois.readObject();
ois.close();
fis.close();

----------------------------------------------------------------------------------------------------------------

The repetition of the strings are identified and removed and the storage file size reduces to ~600 KB, where as a normal serialization, without using deflator/inflator, would create a file of ~6MB.

But the problem here is, when the number of entries increases, the Java run time is not able to handle the growing size of the HashMap during runtime.
Is there any effecient ways where, right at the time of constructing the hashmap itself, to identify the data repetition and avoid it, and the HashMap construction is done memory effeciently?

regards
mad
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Moving this to Java in general since it has nothing to do with SCJP

withing a predefined set of ten to 15 strings


Sounds like a job for an enum. Even so, if the string references refer to the same value instead of creating new objects for each insertion, the size that is maintained in memory would reduce.

What exactly are you trying to achieve with the HashMap ? Can you elaborate on your use case ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Why are there two of these threads? I answered over here. Let's pick one thread and stick with it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
Closing this topic. Let's continue the discussion in the other one.


Ping & DNS - my free Android networking tools app
 
GeeCON Prague 2014
 
subject: Optimising size of HashMap in runtime