Hi Tikeswar,
I couldn't find any reference to weak interfaces (except for the fluid
mechanics), but I found a "java.util.WeakHashMap" class in the Java
documentation ("http://java.sun.com/javase/6/docs/api/").
According to the documentation, a WeakHashMap is different from a
"normal" HashMap as the WekHashMap doesn't count as a reference to a key
from the point of view of the garbage collector.
Normally, a HashMap contains a reference to the keys and their
associated values. For e.g. hash.put("John", "Smith") adds a "Smith"
associated to the key "John". If there is no other String "John" used in
the program, the pair ("John","Smith") still remains in the HashMap. If
the user enters a String that happens to be "John" in the String
variable "key" and hash.get(key) is called, then the associated "Smith"
is returned at any later moment.
According to the documentation (I never used), a WeakHashMap allows the
garbage collector to discard a key if there is no other reference to
that key in the program (except for the WeakHashMap itself). It means
that if the user entered "John" and "Smith" into two local variables (of
the method), and the hash.put(key, value) method was called, and the
variable "key" became out of scope (i.e. the method ends) and there is
no other reference to "John" in the program, it is possible that the
pair ("John", "Smith") is silently removed from the WeakHashMap (it
cannot be retrieved at a later moment).
I think I can imagine the use of this kind of constructs. For e.g. one
assigns listeners to some important pieces of data represented as
JavaBeans in the program, so that if the user modifies a JavaBean in one
window, then the new values to be displayed in all the windows that
display the JavaBean. That implies storing a list of windows associated
to each JavaBean in some HashMap (the JavaBean is the key, the list of
windows is the value). When a JavaBean is no longer in use, it still
remains in the HashMap unless some VERY careful programming properly
remove it. Using a WeakHashMap avoids to use the word "VERY" in
capitals, the garbage collector does the cleaning instead.
I actually had this sort of need but, stupidly, I didn't use a
WekHashMap (I didn't know it exists)!
Nice question!
Hope it helps
mihai
Tikeswar Mohanty a écrit :
hi i want to know that what is weak interface and weak hashmap. --
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to
this email with the words "REMOVE ME" as the subject.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.