Gishantha Darshana
Gishantha Darshana

Reputation: 163

Removing element from ArrayList<HashMap<String, String>>

Hi I have this array list of hashmaps

[{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}]

I want to check particular entry from here using "AppoinmentID" and i want to get that record for a diferent hashmap and all the others to a different one.. how can I do it? Thanks in advance.

Upvotes: 0

Views: 2899

Answers (3)

Philipp Sander
Philipp Sander

Reputation: 10249

storing these values in a hashmap is not a good idea. why don't you create an appointment class. removing an appointment object will be easy in this case.

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;
    }

}

now if you want to delete a specific item with a certain ID:

List<Appointment> appointments = new ArrayList<Appointment>();
appointments.remove(new Appointment(theIdYouWantToDelete));

or an even better way:

Store them like this

Map<Integer, Appointment> appointments = new HashMap<Integer, Appointment>();
appointments.put(appointment.getAppointmentId(), appointment);

and remove them like this:

appointments.remove(theIdYouWantToDelete);

with this approach, you do not need the equals method.

Why it works:

When you want to remove an Object from a List or a Map, Java uses the equals method to identify them. As You can see i only check for the appointmentId. So if the IDs of 2 object are the same, Java says they are the same object. If you don't override equals, check only checks for == (same object in the memory) which mostly isn't the case.

Upvotes: 1

Aromal Sasidharan
Aromal Sasidharan

Reputation: 678

1.Create a class

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. Creating Appointment Object and Storing in 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. deleting an Appointment Object

 appointments.remove(aAppointmentId)

4. getting an Appointment Object

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));

Upvotes: 0

vipul mittal
vipul mittal

Reputation: 17401

You can have class:

public class Apointment{
Stirng EndTime="09:00 AM";
int AppointmentId=79;
...
...
}

and have one hashmap with apointmentId as key

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

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

And if you have apointmentID you can get apointment object by:

Apointment ap=map.get(79);

Upvotes: 0

Related Questions