妙用Map

--------------------------------------------------------------------------------------------

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文章版權屬於作者,受法律保護。沒有作者書面許可不得轉載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章