Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Initialize Map size

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Initialize Map size

    In my class I create about 10 different Maps. I thought of initializing the size. But I also thought is there going to be an advantage in doing so.

    Approach1:
    Map<String,String> myMap = new HashMap<String, String>(5);
    Approch2:
    Map<String,String> myMap = new HashMap<String, String>();
    Per Approach2, the default capacity is 16. But will the memory allocation is more for approach2 compare to approach1?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Initialize Map size

    will the memory allocation is more for approach2
    What Java programmer cares? If you're interested, you can try it both ways and inspect the heap and memory usage with a tool like jvisualvm. Unless you're writing code that's at the core of a high-throughput application, you're unlikely to feel the benefit of such a hand optimisation. Additionally, you may not see the same difference in memory allocation from one JVM to another, or possibly - thanks to the unpredictability of the garbage collector - even from one run of your code to another.

    While you're writing applications with 10 Maps each containing 5 entries, burn memory with gay abandon - that's what makes us Java programmers!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Initialize Map size

    Thanks Sean4u. I fully agree with you...I just wanted to have opinion of one more person.

Similar Threads

  1. Re-Initialize array without loosing contents..
    By Mr.777 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 17th, 2011, 05:47 AM
  2. Replies: 21
    Last Post: May 29th, 2011, 12:48 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  5. I'm not sure how to initialize GraphicsDevice and Window
    By DotChris in forum AWT / Java Swing
    Replies: 3
    Last Post: July 15th, 2009, 09:00 AM