How to Use the removeall method of collection classes such as Java list

Source: Internet
Author: User

When I see this title, it is estimated that many people will wonder that the removeall method of the Collection class is not used as a programmer.

 

Okay, I admit that I did not use this method. despise me. O (partition _ partition) o ha!

 

Post a question first --->

 

Entity class (User ):

Public class user {</P> <p> private string name; <br/> private int age; </P> <p> // setter and getter <br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public int getage () {<br/> return age; <br/>}< br/> Public void setage (INT age) {<br/> This. age = age; <br/>}</P> <p>}

 

Test collection class (userlist ):

Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class userlist {</P> <p> private list <user> sublist; <br/> private list <user> alllist; </P> <p> Public userlist () {<br/> sublist = new arraylist <user> (); <br/> alllist = new arraylist <user> (); </P> <p> for (INT I = 0; I <3; I ++) {<br/> User user = new user (); <br/> User. setage (I); <br/> User. setname ("lyh" + I); <br/> sublist. add (User); <br/>}</P> <p> for (INT I = 0; I <10; I ++) {<br/> User user = new user (); <br/> User. setage (I); <br/> User. setname ("lyh" + I); <br/> alllist. add (User); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> userlist = new userlist (); <br/> userlist. alllist. removeall (userlist. sublist); // call the removeall method </P> <p> system. out. println (userlist. alllist. size (); </P> <p >}< br/>}

 

What is the final result of the print? 7? That's wrong !! The result is 10.
.

 

Why? Is there a problem with the removeall method?

 

This is the question recently encountered when removeall was used (of course, I simply abstracted the actual problem ). At that time, I was puzzled, and even naive thought it was a Java bug. I don't know if it's my head bug!

 

 

Cause analysis:

 

First look at the API

Boolean removeall (collection <?> C) </P> <p> remove all the elements contained in the specified collection from the list (optional ).

That's right. It is to remove the elements contained in the child set. Why isn't it 7, but 10.

Let's take a look at the API description and remove all its elements. Note this word: Include!

 

When executing the removeall method, we will first compare the elements in the set. If the elements are equal, we will perform the removal operation. As a result, I believe many people have understood what is going on, because equals is not equal, NO removal is performed.

 

Why is the object class attributes obviously the same when they are not equal?

 

Because the entity class does not have the override hashcode and equals methods! When executing the removeall method, the equals method is used to determine whether the set elements are equal. If the override equals method is not used, the default comparison object is still used, so the above problem will occur!

 

In the final analysis, the foundation is still not well mastered, and I also reminded myself that when writing entity classes, I should try to override hashcode and equals methods as much as possible. Otherwise, problems may occur someday.

 

After the problem is summarized, of course, this problem is actually very simple. I only hope to help my friends who have the same questions.

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.