Java Collection framework: Some tips you may not know

Source: Internet
Author: User

 

 

You may already know the following facts about the Java Collection framework:

  1. The vector is thread-safe, and the arraylist is not
  2. 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.
  3. 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)
  4. Hashmap is thread-safe, and hashtable is thread-safe.

However, you may not know or understand these things:

  1. The producer list is not thread-safe.
  2. Arraylist is much faster than vector in a multi-thread or single-thread environment.
  3. Null can be inserted in set, but only one
  4. Null can be inserted in hashmap, but not in hashtable.
  5. Treeset uses compareto instead of equals to determine whether two elements are equal
  6. Elements in hashmap, whether keyset or valueset, are not necessarily sorted by the insertion time. The same is true for hashset.
  7. For non-thread-safe classes, such as hashmap, arraylist, and treeset, you can use the collections. synchronizedxxx method (such as synchronizedset, synchronizedlist, and synchronizedmap)
  8. You can use collections. unmodifiablelist to create a list that cannot be modified.
  9. You can use arrays. equals to determine whether two arrays are equal.
  10. 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 ).
  11. 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 ">?
  1. Public class testhashmap {
  2. Public static weakhashmap whmp = new weakhashmap ();
  3. Public static void main (string ARGs []) {
  4. String S1 = new string ("one ");
  5. Whmp. Put (S1, new INTEGER (1 ));
  6. Addobject (whmp );
  7. Set sethmp = whmp. keyset ();
  8. For (iterator it = sethmp. iterator (); it. hasnext ();){
  9. System. Out. println (whmp. Get (it. Next ()));
  10. }
  11. }
  12. Public static void addobject (weakhashmap whmp ){
  13. String S2 = new string ("two ");
  14. String S3 = new string ("three ");
  15. Whmp. Put (S2, new INTEGER (2 ));
  16. Whmp. Put (S3, new INTEGER (3 ));
  17. S2 = NULL;
  18. S3 = NULL;
  19. System. GC ();
  20. }
  21. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.