Java Pit-----Repeating the same object in the list

Source: Internet
Author: User

The Add method of list in Java does not check whether the list contains items that are joined

The Add method of list in Java does not check whether the list contains items that are joined

The Add method of list in Java does not check whether the list contains items that are joined

Important thing to say three times

The list adds a reference and does not check to see if it has been added to the list

This also means: if you manage a bunch of recycled objects, the list should not be chained to the list and should be a stack, otherwise, as the chain pushes the objects that are actually available to be squeezed into the garbage heap and then recycled, the listing looks small but actually only a few actual reference objects, Any action on the object, such as setting a visible property or setting a value, can have an immeasurable effect on the use of the list.

And each time you add a list, if you have a unique requirement for the elements in the list (usually there), you need to check if it already exists in the list.

The test code is attached:

1Fucker B =Newfucker ();2Fucker A =Newfucker ();3 a.oops ();4 b.oops ();5 6list<fucker> lbs =NewArraylist<>();7 8          for(intI= 0;i<10;i++) {9 Lbs.add (a);Ten Lbs.add (b); One         } A  - A.fuck (); -          for(inti=0;i<10;i++) { theLbs.get (0). Fuck (); -Lbs.remove (Lbs.get (0)); -Lbs.get (0). Fuck (); -Lbs.remove (Lbs.get (0)); +}

Fucker:

1  Public classFucker {2     Private intValue=0;3 4      Public voidfuck () {5System.out.println ("Oooo!");6 System.out.println (value);7Value = 0;8     }9 Ten      Public voidOops () { OneValue = 1; ASystem.out.println ("changed!"); -     } -  the}

In fact, generally this kind of object should be not management, but brother does not trust Java garbage collection mechanism, and I firmly believe that he setvisible (View.gone) that the View does not exist in the development of the method will be a destructive barrier, so the compromise chose a bit ...

Java Pit-----Repeating the same object in the list

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.