嗨,我有这个哈希图的数组列表
[{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和其他所有其他的记录...我怎么能这样做?提前谢谢。
答案 0 :(得分:1)
将这些值存储在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));
或更好的方式:
像这样存储它们
Map<Integer, Appointment> appointments = new HashMap<Integer, Appointment>();
appointments.put(appointment.getAppointmentId(), appointment);
并将其删除:
appointments.remove(theIdYouWantToDelete);
使用这种方法,您不需要equals方法。
为什么会这样:
如果要从List或Map中删除Object,Java会使用equals方法来识别它们。正如您所见,我只检查appointmentId
。因此,如果2个对象的ID相同,则Java表示它们是同一个对象。如果你不重写equals,只检查==
(内存中的同一个对象)的检查,但大多数情况并非如此。
答案 1 :(得分:0)
你可以上课:
public class Apointment{
Stirng EndTime="09:00 AM";
int AppointmentId=79;
...
...
}
并且有一个带有apointmentId作为键的
的hashmapHashMap<Integer,Apointment> map=new HashMap<Integer,Apointment>();
Apointment ap=new Apointment(...);
map.put(ap.getAppointmentId(),ap);
..
..
..
如果你有apointmentID,你可以通过以下方式获得apointment对象:
Apointment ap=map.get(79);
答案 2 :(得分:0)
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;
}
}
2。创建约会对象并在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);
3。删除约会对象
appointments.remove(aAppointmentId)
4。获得约会对象
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));