2012-03-21 108 views
0

我想從特定日期的哈希表中刪除數據,哈希表的類型(字符串,矢量)。最初我檢查如果散列表包含usrDate如果是的話我需要刪除所有的數據frm散列表僅用於usrDate,並添加新的數據是listEvents.But其他日期的散列表中的包含不應該被刪除。如何從特定日期的哈希表中刪除數據

listEvents.addElement(eventBean);//new data is here 
for (int i = 0; i < listEvents.size();i++) { 
    EventData e = (EventData)listEvents.elementAt(i); 
} 

//checking if hashtable has given date 
if (listUserEvents.containsKey(usrDate)) { 
    Vector info = (Vector)listUserEvents.get(usrDate); 

    info.addElement(eventBean); 
    listUserEvents.put(usrDate,info); 
} else {            
    listUserEvents.put(usrDate,listEvents); 
} 

我只是想的listEvents添加到哈希表在指定日期的,不影響Hashtable將有一些其他的日期數據的其他數據。

private Hashtable getEvents(String usrDate, String timezone) { 
       try { 

         listUserEvents = getUserInfo(); 
         listEvents = new Vector(); 

         Calendar calendarLocalEvent = Calendar.getInstance(); 
         // fetches time zone 
         TimeZone timeZoneEvent = calendarLocalEvent.getTimeZone(); 
         System.out.println("Time Zone first-->" 
             + timeZoneEvent.getDefault()); 
         if (timezone.equals(timeZoneEvent.getDefault())) { 
           ; 
         } else { 
           TimeZone timeZoneChange = TimeZone.getTimeZone(timezone); 
           System.out.println("Time Zone change-->" +timeZoneChange); 
           Device.setTimeZone(timeZoneChange); 

         } 
         EventList eventList = (EventList) PIM.getInstance().openPIMList(
             PIM.EVENT_LIST, PIM.READ_ONLY); 

         Enumeration events = eventList.items(); 

         while (events.hasMoreElements()) { 
           System.out.println("in while"); 
           Event event = (Event) events.nextElement(); 

           if (eventList.isSupportedField(Event.START) 
               && event.countValues(Event.START) > 0) 
{ 
             long start = event.getDate(Event.START, 0); 
             SimpleDateFormat sdf = new SimpleDateFormat(
                 "MMM dd,yyyy HH:mm"); 
             SimpleDateFormat sdf_start = new SimpleDateFormat("HH:mm"); 
             SimpleDateFormat sdf_start_min = new SimpleDateFormat("HH"); 
             String dateString = sdf.formatLocal(start); 
             String timeString = sdf_start.formatLocal(start); 
             String hour = sdf_start_min.formatLocal(start); 
             SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd,yyyy"); 
             String date = sdf1.formatLocal(start); 
             System.out.println("dates are :" + date +"user" + usrDate); 
             if (usrDate.equalsIgnoreCase(date)) { 
               System.out.println("dates are equal:" +date); 
               EventData eventBean = new EventData(); 

               if (eventList.isSupportedField(Event.END) 
                   && event.countValues(Event.END) > 0) { 
                 long end = event.getDate(Event.END, 0); 
                 SimpleDateFormat sdform = new SimpleDateFormat(
                     "MMM dd, yyyy HH:mm"); 
                 SimpleDateFormat sdfTime = new SimpleDateFormat(
                     "HH:mm"); 
                 SimpleDateFormat sdfhr = new SimpleDateFormat("HH"); 
                 String dateString1 =sdform.formatLocal(end); 
                 String timeString1 =sdfTime.formatLocal(end); 
                 String hr = sdfhr.formatLocal(end); 
                 eventBean.setStartHr(hour); 
                 eventBean.setEndHr(hr); 
                 eventBean.setStartTime(timeString); 
                 eventBean.setEndTime(timeString1); 
                 eventBean.setStartDate(dateString); 
                 eventBean.setEndDate(dateString1); 

                 // Dialog.alert("equal startdate" + dateString); 
                 // Dialog.alert("equal end date"+ dateString1); 
               } 
               if (eventList.isSupportedField(Event.LOCATION) 
                   && event.countValues(Event.LOCATION) > 0) { 
                 String location = event 
                     .getString(Event.LOCATION, 0); 
                 eventBean.setLocation(location); 
                 // Dialog.alert("equal location"+ location); 
               } 
               if (eventList.isSupportedField(Event.SUMMARY) 
                   && event.countValues(Event.SUMMARY) > 0) { 
                 String subject = event.getString(Event.SUMMARY, 0); 
                 eventBean.setSummary(subject); 
                 // Dialog.alert("equal subject" +subject); 
               } 
               eventBean.setUserDate(usrDate); 
               eventBean.setTimeZone(timezone); 
               listEvents.addElement(eventBean); 
               System.out.println("the size bf hashis" 
                   + listEvents.size()); 
               for (int i = 0; i < listEvents.size();i++) { 
                 EventData e = (EventData)listEvents.elementAt(i); 
                 System.out.println("so thesummary is ::" 
                     + e.getSummary()); 
               } 
             // for(int i=0;i<listUserEvents.size();i++){ 
               if (listUserEvents.containsKey(usrDate)) { 

                // listUserEvents.remove(usrDate); 
                 Vector info = (Vector)listUserEvents.get(usrDate); 
                 System.out.println("the size in getEvents is" 
                     + info.size()); 
                 if(info.contains(usrDate)){ 
                  System.out.println("in info"); 
                 } 
                 info.addElement(eventBean); 
                 System.out.println("vector size info is" 
                     + info.size()); 
                 listUserEvents.put(usrDate,info); 
               } else { 
                 System.out.println("in else of getevent" +listEvents.size()); 
                 listUserEvents.put(usrDate,listEvents); 
               } 
             // } 

             } else { 
               // Dialog.alert("not equal"); 
             } 

           } 

         } 
         Device.setTimeZone(timeZoneEvent); 
         Calendar calendarLocalLastEvent = Calendar.getInstance(); 
         // fetches time zone 
         TimeZone timeZoneEventLast =calendarLocalLastEvent.getTimeZone(); 
         System.out.println("Time Zone last-->" 
             + timeZoneEventLast.getDefault()); 

       } catch (PIMException e) { 
         // //Dialog.alert(e.getMessage()); 
       } 
       System.out.println("size in hashtable " + listUserEvents.size()); 
       return listUserEvents; 

     } 
+0

那麼,無論你的散列表鍵是否必須是日期,或者你需要迭代所有的散列表條目。如果你想刪除一個條目,使用'remove(key)'將其刪除。 – 2012-03-21 18:24:32

+0

你的問題不清楚。你在哪裏面臨這個問題? – 2012-03-21 18:27:00

+0

@HotLicks我已經使用刪除(鍵),但它的功能工作,你可以請修改的代碼段和示例 – 2012-03-21 18:29:49

回答

1

它必須是這樣的

for(int i = 0; i<listUserEvents.size();i++) 
{ 

    if (listUserEvents.containsKey(usrDate)){ 

    listUserEvents.remove(usrDate); 

    } 

}

+0

它不能正常工作我已經在這裏複製了整個函數http://codepad.org/6hFplePX – 2012-03-21 19:12:40

+0

我在這裏粘貼了代碼 – 2012-03-21 19:15:04

+0

你可以用main方法粘貼整個類嗎?我認爲這將有助於我和他人在主要職位 – bentham 2012-03-21 19:30:08

0

這裏是如何工作的一個簡單的例子:

Hashtable<String, Integer> numbers = new Hashtable<String, Integer>(); 
    numbers.put("one", 1); 
    numbers.put("two", 2); 

    if (numbers.containsKey("two")) { 
     numbers.put("two", 222); 
    } 

什麼是你在遇到困難嗎?另外你的鑰匙是什麼類型的?是java.util.Date還是別的?

+0

散列表是類型(字符串向量) – 2012-03-21 18:56:31

+0

我有代碼somethinh在http://codepad.org/6hFplePX,刪除是給出例外 – 2012-03-21 19:05:53

+0

我已粘貼碼 – 2012-03-21 19:15:22