"Java" for ArrayList to go heavy

Source: Internet
Author: User

ArrayList does not have a well-packaged deduplication method, for example, for a ArrayList of [2, 5, 2, 3, 2, 4], I want to remove the duplicate elements,

I do not want to put the statement also so long, do not want to use for the method of the loop to heavy, then you can consider the ArrayList into a temporary hashset, and then the temporary HashSet converted back to ArrayList,

Because the elements inside the hashset are not repeatable! As for what is ArrayList and HashSet, in "Java" Java Collections Class--java in the upgraded version of the data structure (click to open the link) has been made clear, here no longer repeat.

You can write this:

hashset<integer> hashset_temp = new hashset<integer> (arraylist); ArrayList  = new Arraylist<integer > (hashset_temp);

can also be written more concise, even the temporary hashset_temp variables are not:

ArrayList  = new arraylist<integer> (new Hashset<integer> (ArrayList));

After that, the elements of ArrayList become [2, 3, 4, 5]

"Java" for ArrayList to go heavy

Related Article

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.