Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Struts - A MVC web framework
Tomcat - JSP/Servlet container
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
JSP - A mailing list about Java Server Pages specification and reference
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Oracle Connection Pooling in 3 2 2
Servlet : Session invalidate
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Tomcat and webapplication specific java library path
Running a Simple JMS Example
Mapping in workers2 properties
org apache jasper JasperException
Cannot find message resources under key org apache struts action
   MESSAGE
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
url string for connecting jboss to oracle
Value attribute of <html:checkbox
javax servlet ServletException: BeanUtils populate
HTTP Status 404 The requested resource is not available
5 0 18: Windows XP Pro vs Windows 2000
 
' Related to HashMap OR HashTable

' Related to HashMap OR HashTable

2004-07-28       - By Sanjeev Chakravarty

 Back
Hi Manish,

Though this is not the right forum to discuss about HashMap which is purely
a Java topic, embedded is my explanation on why you don't get the exception.

Sanjeev Chakravarty
Sr. Consultant


Manish Malhotra <manish.mmalhotra@(protected)>
Sent by: "A mailing list for Java(tm) 2 Platform, Enterprise Edition"
<J2EE-INTEREST@(protected)>
07/27/2004 09:08 AM
Please respond to manish.mmalhotra
       To:        J2EE-INTEREST@(protected)
       cc:
       Subject:        ' Related to HashMap OR HashTable



Hi,
I know its not purely J2EE issue but, I have a doubt regarding the Map's
func. in Java.

HashMap have one constructor as HashMap(int cap, float loadFactor)
which says that create a HashMap with this much capacity and this
loadFactor.
And its threshold value is (cap*loadFactor) after which it will reHash the
Map.
> Javadoc details further :
> The capacity is the number of buckets in the hash table, and the initial
capacity is simply the capacity
>at the time the hash table is created. The load factor is a measure of how
full the hash table is allowed
> to get before its capacity is automatically increased. When the number of
entries in the hash table exceeds
>the product of the load factor and the current capacity, the capacity is
roughly doubled by calling the rehash method.

But actually Map's const code is like this :
  public HashMap(int initialCapacity, float loadFactor) {
      if (initialCapacity < 0)
          throw new IllegalArgumentException("Illegal initial capacity: "
+
                                             initialCapacity);
      if (initialCapacity > MAXIMUM_CAPACITY)
          initialCapacity = MAXIMUM_CAPACITY;
      if (loadFactor <= 0 || Float.isNaN(loadFactor))
          throw new IllegalArgumentException("Illegal load factor: " +
                                             loadFactor);

      // Find a power of 2 >= initialCapacity
      int capacity = 1;
      while (capacity < initialCapacity)
          capacity <<= 1;

      this.loadFactor = loadFactor;
      threshold = (int)(capacity * loadFactor);
      table = new Entry[capacity];
      init();
  }

So its making capacity as power of 2.

And put method resize only if there is data more then the threshold is try
to add.

Now if I Create HashMap(1, 2.0f) const.
> A load factor of greater than 0.75 is not efficient. Increasing the load
factor
> will increase the number of collisions in inserting/searching an entry. It is
> important to understand the concept of buckets. In the above creation of
HashMap
> there will be a single bucket created and after the second entry is added
to the
> same bucket (entries in the same bucket are represented as a linked list)
then
> there will be resizing of the internal table

Its capacity would be
cap  = 1*2 = 2
> capacity will remain 1

Its threshold would be
th = 2.0 * 2 = 4;
> threshold will be 2

And the size of the array of Entry would be 2. (Internally HashMap uses
Entry to maintain the objects key, value)
> The size of the Entry array will be 1

Hence, If I add more then 2 entries in this HashMap then how it worked
because before the threeshold comes it wont resize
and the inti array of Entry class is size of 2, So third should give
exception ...... I know its handeled but I dnt kn why?
> Since the second entry is added to the same bucked as a linked list we
> don't get an exception

Its interesting.... So please explain.......
> I hope you will now be able to comprehend better the way HashMap works.
> You can find more indepth analysis at:
> http://java.sun.com/docs/books/tutorial/collections/TOC.html#implementations

regards,
Manish

===========================================================================
To unsubscribe, send email to listserv@(protected) and include in the body
of the message "signoff J2EE-INTEREST".  For general help, send email to
listserv@(protected) and include in the body of the message "help".

©2008 junlu.com - Jax Systems, LLC, U.S.A.