码迷,mamicode.com
首页 > 编程语言 > 详细

java好习惯2

时间:2016-08-12 11:40:55      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

1.这么拼参数的

学习一下,当keys.length-1是加上"&"而且是在拼串的后面

public static String map2Content(Map params){
  StringBuilder stringBuilder=new StringBuilder();
  try {
    Set keySet=params.keySet();
    Object[] keys=keySet.toArray();
    for (int i=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 new HttpException("failed to generate content from map",e);
  }
  return stringBuilder.toString();
}

2.如果文件存在就删除,再创建一个新文件

private void saveUsers(Set<String> set,String fileName){
  File f=new File(getDataFolder(),fileName);
  if (f.exists()) {
    f.delete();
  }
  try {
    f.createNewFile();
    BufferedWriter bWriter=new BufferedWriter(new FileWriter(f));
    int count=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如果是这个的实例,强转成这个类,常用的写法

}

@Override public Set<Activity> ge
tInstitutionActivities(){ Set
<Activity> activities=new HashSet<Activity>(); for (int i=0; i < institutionActivities.getWidgetCount(); i++) { Widget widget=institutionActivities.getWidget(i); if (widget instanceof CheckBox) { CheckBox cb=(CheckBox)widget; if (cb.getValue()) { Activity activity=new Activity(); activity.setId(Integer.parseInt(cb.getFormValue())); activity.setName(cb.getText()); activities.add(activity); } } } return activities; }

4.set中添加元素而且不重复

private Object createStringSet(String... strings){
  Set<String> stringSet=new HashSet<String>();
  for (  String string : strings) {
    stringSet.add(string);
  }
  return stringSet;
}

5.异常时可以返回null,为空可以返回null

public static String 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));
      }
    }
    return queryString;
  }
  return null;
}

6.

 

java好习惯2

标签:

原文地址:http://www.cnblogs.com/lonely-buffoon/p/5764121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!