Remove the duplicate values in Java code

Source: Internet
Author: User

Upload the code First,

1List<String>CriteriaList = new ArrayList<String>();2 Efsncompanycriteria Companycriteria = new Efsncompanycriteria (User.getcompanyid (),3 Efsncompanycriteria. CRITERIA1,4 con);5 Efsncompanycriteria Mpcompanycriteria = new Efsncompanycriteria (User.getcompanyid (),6 Efsncompanycriteria. CRITERIA2,7 con);8 Efsncompanycriteria Seccompanycriteria = new Efsncompanycriteria (User.getcompanyid (),9 Efsncompanycriteria. CRITERIA3,Ten con); One Criterialist.addall (Companycriteria.getlistofplanlevels ()); A Criterialist.addall (Mpcompanycriteria.getlistofplanlevels ()); -Criterialist.addall (Seccompanycriteria.getlistofplanlevels ());

As mentioned above, there may be duplicate values in the 3 criteria that are marked blue, so when we add them all to criterialist, we need to take into account the problem of duplicate values when we show on interface.

Start thinking about it, with the IF (...) {Criterialist.add (...)} Method can be difficult to achieve and error prone.

Then I thought about set (the simplest kind of collection, the objects in the collection are not sorted in a particular way, and there are no duplicate objects .) )

Therefore, the following code block is used to resolve this problem.

1 hashset<string> criterset  new hashset<string>(criterialist);
New sets the Set object, and then puts the above criterialist into the inside, although there are duplicate elements in the criterialist, but this time to get
There is no duplicate value in the Criterset, do not know if it is set characteristics (in the internal processing mechanism), tomorrow asked the company of the Great God.
2 criterialist.clear (); 3 Criterialist.addall (Criterset);
Clear () and AddAll ().

Very simple and clear code.

Remove the duplicate values in Java code

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.