java.util..get(
key)
-
get get( key)
Returns the value to which the specified key is mapped, or
null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a
value v such that (key==null ? k==null :
key.equals(k)), then this method returns v; otherwise it
returns null. (There can be at most one such mapping.)
If this map permits null values, then a return value of null
does not necessarily indicate that the map contains no mapping for the
key; it's also possible that the map explicitly maps the key to
null. The
operation may be used to distinguish these two cases.
Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which the specified key is mapped, or null if this
map contains no mapping for the key
Throws:
-
if the key is of an inappropriate type for this map ()
- if the specified key is null and this map does not permit null keys ()
表结构如下:
create table user(
id int not null primary key,
roleid int not null ,
deptid int not null,
tlid int null,
email varchar(50) not null,
password varchar(50) not null
);
当使用map的get方法时抛出null异常,
- if (map.get("tlid")!=null) {
-
user.setTlid((Integer)map.get("tlid"));
-
}
将代码改为以下:显示正常。
- if(map.containsKey("tlid")){
-
//if (!map.get("tlid").equals(null)) {
-
if (map.get("tlid")!=null) {
-
user.setTlid((Integer)map.get("tlid"));
-
}
-
-
}
以下两句代码的区别:
- if (!map.get("tlid").equals(null)) {
当
map.get("tlid")为null时,抛出空指针异常。正确写法:
- if (map.get("tlid")!=null) {
完整代码:
- private User populate(Map<String, Object> map) {
-
User user = new User();
-
user.setId((Integer) map.get("id"));
-
user.setRoleid((Integer) map.get("roleid"));
-
user.setDeptid((Integer)map.get("deptid"));
-
/*
-
* foreign key..,no map.. omit these attr
-
*/
-
if(map.containsKey("tlid")){
-
//if (!map.get("tlid").equals(null)) {
-
if (map.get("tlid")!=null) {
-
user.setTlid((Integer)map.get("tlid"));
-
}
-
-
}
-
user.setEmail((String) map.get("email"));
-
user.setPassword((String) map.get("password"));
-
-
return user;
-
}
阅读(7037) | 评论(0) | 转发(0) |