如何从特定日期的哈希表中删除数据

时间:2012-03-21 18:18:05

标签: java vector hashtable

我正在尝试从哈希表中删除特定日期的数据,哈希表是类型(字符串,向量)。最初我正​​在检查哈希表是否包含usrDate如果是,那么我需要删除所有数据frm哈希表仅用于usrDate并添加listEvents的新数据。但不应删除其他日期的哈希表包含。

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添加到给定日期的哈希表中,而不会影响哈希表中包含其他日期数据的其他数据。

 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;

       }

2 个答案:

答案 0 :(得分:1)

必须是这样的

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

    if (listUserEvents.containsKey(usrDate)){

     listUserEvents.remove(usrDate);

    }

}

答案 1 :(得分: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);
    }

你有什么困难?此外,Hashtable键的类型是什么?是java.util.Date还是其他什么?