Android 正在从ArrayList中删除元素<;HashMap<;字符串,字符串>&燃气轮机;

Android 正在从ArrayList中删除元素<;HashMap<;字符串,字符串>&燃气轮机;,android,arraylist,hashmap,Android,Arraylist,Hashmap,嗨,我有这个哈希映射数组列表 [{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=79, Date=11/18/13}, {EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=80, Date=11/18/13}, {EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=81

嗨,我有这个哈希映射数组列表

[{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=79, Date=11/18/13},
{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=80, Date=11/18/13},
{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=81, Date=11/18/13},
{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=82, Date=11/18/13},
{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=83, Date=11/18/13},
{EndTime=09:00 AM, UserId=48, StartTime=08:00 AM, AppointmentId=85, Date=11/18/13}]
我想从这里使用“AppoinmentID”检查特定的条目,我想获得不同hashmap的记录,并将所有其他记录转换为不同的记录。。我怎么做?提前感谢。

您可以上课:

public class Apointment{
Stirng EndTime="09:00 AM";
int AppointmentId=79;
...
...
}
并且有一个以apointmentId为键的hashmap

HashMap<Integer,Apointment> map=new  HashMap<Integer,Apointment>();
Apointment ap=new Apointment(...);

map.put(ap.getAppointmentId(),ap);
..
..
..

将这些值存储在hashmap中不是一个好主意。为什么不创建一个约会类呢。在这种情况下,删除约会对象将很容易

public class Appointment
{
    private int     appointmentId;
    private int     userId;     // or private User user
    private Date    start;
    private Date    end;

    public Appointment(int id)
    {
        this.appointmentId = id;
    }

    // getters and setters

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (this.getClass() != obj.getClass())
        {
            return false;
        }
        Appointment other = (Appointment) obj;
        if (this.appointmentId != other.appointmentId)
        {
            return false;
        }
        return true;
    }

}
现在,如果要删除具有特定ID的特定项目:

List<Appointment> appointments = new ArrayList<Appointment>();
appointments.remove(new Appointment(theIdYouWantToDelete));
使用这种方法,您不需要equals方法

工作原理:

当您想从列表或映射中删除对象时,Java使用equals方法来标识它们。如您所见,我只检查
任命ID
。因此,如果2个对象的ID相同,Java会说它们是相同的对象。如果不重写equals,则只检查
===
(内存中的同一对象),而大多数情况并非如此。

1.创建一个类

public class Appointment
{
 public int     appointmentId;
 public int     userId;    
 public Date    startTime;
 public Date    endTime;
 public Appointment(int id,int aUserID,Date aStartTime,Date aEndTime)
 {
    this.appointmentId = id;
    this.userId = aUserID;
    this.startTime = aStartTime;
    thiis.endTime =  aEndTime;
 }
}
二,。创建约会对象并存储在HashMap中

String dateFormat = "MMMM d, yyyy HH:mm"; //any date format
 DateFormat df = new SimpleDateFormat(dateFormat);       
 Date startDate = df.parse("January 2, 2010 13:00");
 Date endDate = df.parse("January 2, 2010 20:00");
 Appointment appointment1 = new Appointment(1,23,startDate,endDate);

 ...
 Map<Integer, Appointment> appointments = new HashMap<Integer, Appointment>();
 // add to hashmap making appointment id as key
 appointments.put(appointment1.appointmentId,appointment1);
 ......
 ...
 appointments.put(appointmentN.appointmentId,appointmentN);
四,。获取约会对象

Appointment ap = appointments.get(aAppointmentId);
System.out.printLn("id "+ ap.appointmentId);
System.out.printLn("userId "+ ap.userId);
DateFormat df = new SimpleDateFormat(dateFormat);       
System.out.printLn("starttime "+  df.format(ap.startTime));
System.out.printLn("endtime "+  df.format(ap.endTime));

imho:将这些值存储在hashmap中不是一个好主意。为什么不创建一个约会类呢。在这种情况下,删除约会对象将很容易。如果你需要一些代码,请告诉我,我会将其作为答案发布。是的……请你给我一些代码:)@PhilippSander关于“将这些值存储在hashmap中不是一个好主意”有什么具体的原因吗?@PankajKumar,正如你所看到的,更难理解handle@PhilippSander请给我一些代码:)请不要公开成员。。。这是一个封装的问题
public class Appointment
{
 public int     appointmentId;
 public int     userId;    
 public Date    startTime;
 public Date    endTime;
 public Appointment(int id,int aUserID,Date aStartTime,Date aEndTime)
 {
    this.appointmentId = id;
    this.userId = aUserID;
    this.startTime = aStartTime;
    thiis.endTime =  aEndTime;
 }
}
String dateFormat = "MMMM d, yyyy HH:mm"; //any date format
 DateFormat df = new SimpleDateFormat(dateFormat);       
 Date startDate = df.parse("January 2, 2010 13:00");
 Date endDate = df.parse("January 2, 2010 20:00");
 Appointment appointment1 = new Appointment(1,23,startDate,endDate);

 ...
 Map<Integer, Appointment> appointments = new HashMap<Integer, Appointment>();
 // add to hashmap making appointment id as key
 appointments.put(appointment1.appointmentId,appointment1);
 ......
 ...
 appointments.put(appointmentN.appointmentId,appointmentN);
 appointments.remove(aAppointmentId)
Appointment ap = appointments.get(aAppointmentId);
System.out.printLn("id "+ ap.appointmentId);
System.out.printLn("userId "+ ap.userId);
DateFormat df = new SimpleDateFormat(dateFormat);       
System.out.printLn("starttime "+  df.format(ap.startTime));
System.out.printLn("endtime "+  df.format(ap.endTime));