Java Good habit 2

Source: Internet
Author: User

1. The spelling of this parameter

Learn, when Keys.length-1 is added "&" and is behind the puzzle

 Public StaticString Map2content (Mapparams) {StringBuilder StringBuilder=NewStringBuilder (); Try{Set KeySet=params. KeySet (); Object[] Keys=Keyset.toarray ();  for(intI=0; i < keys.length; i++{stringbuilder.append (Encode (), keys[i].tostring (),"UTF-8"). Append ("="). Append (Encode (params.Get(Keys[i]). ToString (),"UTF-8")); if (i < (keys.length-1)) {stringbuilder.append ("&"); }    }  } Catch(Exception e) {Throw NewHttpException ("failed to generate content from map", E); }  returnstringbuilder.tostring ();}

2. Delete the file if it exists, and then create a new file

Private voidSaveusers (set<string>set,string FileName) {File F=NewFile (Getdatafolder (), fileName); if (f.exists ()) {f.delete (); } Try {f.createnewfile (); BufferedWriter Bwriter=NewBufferedWriter (NewFileWriter (f)); intCount=0;  for(String name:set) {bwriter.write (name+ System.getproperty ("Line.separator")); Count++; } consoleutils.printinfo (NAME,"Saved" + Count + "users in" + filename+ "'!");    Bwriter.flush ();  Bwriter.close (); } Catch(Exception e) {consoleutils.printexception (E,name,"Error while saving file '" + fileName + "'!"); }}

3.if (widget instanceof Checkbox) {

CheckBox cb = (checkbox) Widget If this is the case, strong turn into this class, commonly used in the wording

}

@Override PublicSet<activity>GE
Tinstitutionactivities () {Set<Activity> activities=NewHashset<activity>(); for(inti=0; I < Institutionactivities.getwidgetcount (); i++) {Widget widget=Institutionactivities.getwidget (i); if (widget instanceof checkbox) {checkbox cb= (checkbox) widget; if(Cb.getvalue ()) {Activity Activity=NewActivity (); Activity.setid (Integer.parseint (Cb.getformvalue ())); Activity.setname (Cb.gettext ()); Activities.add (activity); } } } returnactivities;}

Add elements in 4.set and do not repeat

Private Object createstringset (String ... strings) {  Set<String> stringset=new hashset<string >();    for (  string string:strings) {    stringset.add (string);  }   return Stringset;}

5. The exception can return null, empty can return null

 Public Staticstring getsearchenginequerystring (httpservletrequest request,string referrer) {string queryString=NULL; String HostName=NULL; if(Referrer! =NULL) {URL refererurl; try {refererurl=new URL (referrer); } catch (Malformedurlexception e) {return null; } hostName=Refererurl.gethost (); QueryString=Refererurl.getquery (); if (Strings.isempty (queryString)) {return null; } Set<String> keys=Separams.keyset ();  for(String se:keys) {if(Hostname.tolowercase (). Contains (SE)) {queryString=getquerystringparameter (Querystring,separams.get (SE)); }    }    returnqueryString; }  return NULL;}

6.

Java Good habit 2

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.