You may already know the following facts about the Java Collection framework:
- The vector is thread-safe, and the arraylist is not
- The read operation of arraylist is faster than the append operation of explain list. The append operation of explain list is faster than that of arraylist.
- Set cannot have two equal elements. That is to say, if both E1 and E2 are in the same set, e1.equals (E2) cannot exist)
- Hashmap is thread-safe, and hashtable is thread-safe.
However, you may not know or understand these things:
- The producer list is not thread-safe.
- Arraylist is much faster than vector in a multi-thread or single-thread environment.
- Null can be inserted in set, but only one
- Null can be inserted in hashmap, but not in hashtable.
- Treeset uses compareto instead of equals to determine whether two elements are equal
- Elements in hashmap, whether keyset or valueset, are not necessarily sorted by the insertion time. The same is true for hashset.
- For non-thread-safe classes, such as hashmap, arraylist, and treeset, you can use the collections. synchronizedxxx method (such as synchronizedset, synchronizedlist, and synchronizedmap)
- You can use collections. unmodifiablelist to create a list that cannot be modified.
- You can use arrays. equals to determine whether two arrays are equal.
- Weakhashmap uses the element reference instead of the value as the key, that is, the relevant value can be found only when the reference is the same (A = B ).
- For weakhashmap, the corresponding value does not exist once the reference is invalid, for example, in the following code:
{
Function onclick ()
{
DP. Sh. toolbar. Command ('viewsource', this); Return false;
}
} "Href =" http://www.ondev.net/post/show/565 "> View plain {
Function onclick ()
{
DP. Sh. toolbar. Command ('copytoclipboard', this); Return false;
}
} "Href =" http://www.ondev.net/post/show/565 "> copy to clipboard {
Function onclick ()
{
DP. Sh. toolbar. Command ('printsource', this); Return false;
}
} "Href =" http://www.ondev.net/post/show/565 "> Print {
Function onclick ()
{
DP. Sh. toolbar. Command ('about', this); Return false;
}
} "Href =" http://www.ondev.net/post/show/565 ">?
- Public class testhashmap {
- Public static weakhashmap whmp = new weakhashmap ();
- Public static void main (string ARGs []) {
- String S1 = new string ("one ");
- Whmp. Put (S1, new INTEGER (1 ));
- Addobject (whmp );
- Set sethmp = whmp. keyset ();
- For (iterator it = sethmp. iterator (); it. hasnext ();){
- System. Out. println (whmp. Get (it. Next ()));
- }
- }
- Public static void addobject (weakhashmap whmp ){
- String S2 = new string ("two ");
- String S3 = new string ("three ");
- Whmp. Put (S2, new INTEGER (2 ));
- Whmp. Put (S3, new INTEGER (3 ));
- S2 = NULL;
- S3 = NULL;
- System. GC ();
- }
- }
Public class testhashmap {</P> <p> Public static weakhashmap whmp = new weakhashmap (); </P> <p> Public static void main (string ARGs []) {<br/> string S1 = new string ("one"); <br/> whmp. put (S1, new INTEGER (1); <br/> addobject (whmp); <br/> set sethmp = whmp. keyset (); </P> <p> for (iterator it = sethmp. iterator (); it. hasnext ();) {<br/> system. out. println (whmp. get (it. next (); <br/>}</P> <p> Public static void addobject (weakhashmap whmp) {</P> <p> string S2 = new string ("two"); <br/> string S3 = new string ("three"); <br/> whmp. put (S2, new INTEGER (2); <br/> whmp. put (S3, new INTEGER (3); <br/> S2 = NULL; <br/> S3 = NULL; <br/> system. GC (); <br/>}< br/>
The output is only "one"
Published in: http://www.ondev.net/post/show/565