--------------------------------------------------------------------------------------------
severse中的方法
--------------------------------------------------------------------------------------------
@Override
public Map<String, Integer> getTicketCounts() {
Map<String, Integer> map = new HashMap<String, Integer>();
DetachedCriteria dcNew = DetachedCriteria.forClass(DevSupport.class);
dcNew.setProjection(Projections.count("id"));
dcNew.add(Restrictions.eq("status", Constant.TICKET_STATUS_NEW));
List<Object> listNew = findByCriteria(dcNew);
if (CollectionUtil.isNotEmpty(listNew)) {
map.put(EnumConstant.TicketStatusKey.New.name(), (Integer) listNew.get(0));
}else {
map.put(EnumConstant.TicketStatusKey.New.name(), 0);
}
DetachedCriteria dcOpen = DetachedCriteria.forClass(DevSupport.class);
dcOpen.setProjection(Projections.count("id"));
dcOpen.add(Restrictions.in("status", new Object[]{Constant.TICKET_STATUS_ADMINREPLY,Constant.TICKET_STATUS_DEVELOPERWAIT }));
List<Object> listOpen = findByCriteria(dcOpen);
if (CollectionUtil.isNotEmpty(listOpen)) {
map.put(EnumConstant.TicketStatusKey.Open.name(), (Integer) listOpen.get(0));
}else {
map.put(EnumConstant.TicketStatusKey.Open.name(), 0);
}
DetachedCriteria dcClosed = DetachedCriteria.forClass(DevSupport.class);
dcClosed.setProjection(Projections.count("id"));
dcClosed.add(Restrictions.eq("status", Constant.TICKET_STATUS_CLOSED));
List<Object> listClosed = findByCriteria(dcClosed);
if (CollectionUtil.isNotEmpty(listClosed)) {
map.put(EnumConstant.TicketStatusKey.Closed.name(), (Integer) listClosed.get(0));
}else {
map.put(EnumConstant.TicketStatusKey.Closed.name(), 0);
}
return map;
}
--------------------------------------------------------------------------------------------
在Bean中漂亮的调用
--------------------------------------------------------------------------------------------
public Map<String, Integer> getCountMap() {
if(countMap == null){
return devSupportService.getTicketCounts();
}
return this.countMap;
}
public Integer getNewCount(){
return getCountMap().get(EnumConstant.TicketStatusKey.New.name());
}
public Integer getOpenCount(){
return getCountMap().get(EnumConstant.TicketStatusKey.Open.name());
}
public Integer getClosedCount(){
return getCountMap().get(EnumConstant.TicketStatusKey.Closed.name());
}
-------------------------------------------------------------------------------------------
使用Map时应当注意的细节
-------------------------------------------------------------------------------------------
Map map = new HashMap();
判断map中是否存在某个key时,用方法map.containsKey(key);返回true/false;
遍历map方法
for(String key : map.keySet()){
map.get(key);
}
需求1:
将map中的已存在的所有数据拿出来根据条件改变其中的value,在放回到map中
A:若value的数据类型是 对象 ,则通过map.get(key)获得的是此对象的引用,改变引用的值就改变了原来map里面的值
B:若value是基础数据类型,则通过map.get(key)拿出来的值不能改变原来的map中的值
而如果map中原来已经存在了(key1,value1)如果再用一次map.put(key1,value2)
那么map中存的将是新值,value1将会被覆盖!
.声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。