`

Java开发笔记

    博客分类:
  • Java
阅读更多

1、Map<key,value>的remove方法只能识别相同类型的key值

 

Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1,"a");
map.put(2,"b");
map.put(3,"c");

Short one  = 1;
map.remove(one);
int two = 2;
map.remove(two);

 以上代码最后Map中剩余的值为:1=a,3=c,只有key为2的map被删除掉了。所以如果要删除short one的话必须修改代码为:map.remove(one.intValue())

 

2、Hibernate的HQL不支持原生INSERT操作“INSERT INTO table (xx,xxx) VALUES(?,?)”,只支持“INSERT INTO table (xx,xxx) SELECT aa,bb FROM otherTable ...”,所以如果是向一张表新增数据就只能使用session.save(object)

 

3、两个Date类型的变量可以通过compareTo方法来比较。此方法的描述是这样的:如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。

 

Date dateTime1 = dateFormat.parse(DateStr1);
Date dateTime2 = dateFormat.parse(DateStr2);
int i = dateTime1.compareTo(dateTime2); 

 

4、对象数据类型比较的是内存地址而不是值,如下代码

 

 

Long id1 = Long.valueOf("123");
Long id2 = Long.valueOf("123");
if(id1 == id2){
true;
}else{
false;
}

 上面代码返回的结果为false,而如果要想比较id1和id2的值必须这样写:

 

if(id1.longValue() == id2.longValue()){

}

 

 5、循环删除List内的数据

Iterator<Integer> iterator = ints.iterator(); 
while (iterator.hasNext()) { 
    Integer temp = iterator.next(); 
    if (temp % 3 != 0) { 
        iterator.remove(); 
    } 
} 

 

7、JSTL格式化时间

<fmt:formatDate value="${result}" type="both" dateStyle="full" pattern="yyyy-MM-dd hh:mm" var="createTime"/>

JSP读取:${createTime }

 

8.HttpServletRequest对象

/**
*假如一个请求路径为:http://localhost:8080/demo/apps/demo.jhtml
**/
//返回/demo/apps/demo.jhtml
System.out.println("requestURI="+request.getRequestURI());

//返回http://localhost:8080/demo/apps/demo.jhtml
System.out.println("requestURL="+request.getRequestURL());

//返回/apps/demo.jhtml
System.out.println("servletPath="+request.getServletPath());

 

9.java的split方法,如下格式的字符串,将字符串split转换为数组之后,数组的长度为3而不是4,原因是最后一个@符号后如果是空则不会被计入数组中。

String d = "@2@3@";
String[] s = d.split("@");
System.out.println(s.length);      //输出:3
System.out.println(s.toString());  //输出:[,2,3]

 

10.如下HQL在SqlServer数据库报异常:Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。

String hql = "select distinct new Map(m.id) from Member m order by m.createTime";

 原因是distinct语句后面获取的字段内容必须包含order by的字段,所以select字段中加上createTime就没问题了:

String hql = "select new Map(m.id,m.createTime) from Member m order by m.createTime";

 

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics