Negative code display-do not use array loops to determine whether an object is in a set.

Source: Internet
Author: User

No more nonsense. See the following code:

/*** Determine whether the parameter is in the blacklist * @ Param name indicates the name to be determined * @ return true: in the blacklist */private Boolean inblackname (string name) {string [] blackname = {"black name 1", "blacklist 2", "No Name", "kaokao" ,}; for (INT I = 0; I <blackname. length; I ++) {If (blackname [I]. equals (name) {return true;} return false ;}

In fact, we should record the blacklist in a set to determine whether the set contains the name to be determined.

Private Static final set blacknames = new hashset (); static {blacknames. add ("black name 1"); blacknames. add ("blacklist 2"); blacknames. add ("No Name"); blacknames. add ("kaokao");}/*** determine whether the name is in the blacklist * @ Param name to be determined * @ return true: in the blacklist */private Boolean inblackname (string name) {return blacknames. contains (name );}

Set is used because the blacklist does not require sequential records, which saves storage space.

Set is faster and more convenient than loop judgment.

Of course, a better change to the code is to put the blacklist in the configuration file and transfer it to the memory when the system starts. This is not much to say.

The main purpose of this article is to tell new users not to use array loops to determine whether an object is in a set.

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.