2

I have created a class of Patient object containing patient name and gender and I want to remove it base on Patient name. What is the correct way to do it?

This is my Patient object:

class Patient {

    private String name;
    private int gender;

    public Patient(String name, int gender){
        this.name = name;
        this.gender = gender;
    }

    public String getName(){
        return this.name;
    }

    public int getGender(){
        return this.gender;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setGender(int gender){
        this.gender = gender;
    }

}

This is my Treeset declaration: private TreeSet<Patient> ts = new TreeSet<Patient>(new nameComp());

This is my remove method (I don't know how to start)

void RemovePatient(String patientName) {

}
user3138997
  • 230
  • 1
  • 10
  • 2
    Iterate through your treeset and find the one with the name you want. Then you just remove the element from the set. – Robert Feb 10 '17 at 09:57
  • 1
    Create a patient with that name, and remove it (assuming the badly named nameComp is a comparator only comparing the name of patients). Methods start with a lowercase; classes start with an uppercase. You're doing the reverse. But I have the feeling that what you actually want is a Map. – JB Nizet Feb 10 '17 at 10:04
  • Hi JB Nizet, my nameComp is indeed my comparator to sort my Tree in ascending order base on the patient name. My add Patient function is working correctly but now i am stuck with the delete method base on the name given in the parameter. – user7495150 Feb 10 '17 at 10:11
  • @user7495150 As I said, you probably want a Map. But if you really want to stick with your TreeSet, create a Patient with that name, and remove it from the set. Not sure how I can be clearer. – JB Nizet Feb 10 '17 at 10:23
  • Since we're talking about sorting and `Map`s, I'm going to point out the [`SortedMap`](http://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html) (and its child [`NavigableMap`](http://docs.oracle.com/javase/8/docs/api/java/util/NavigableMap.html)) interface and its two implementations: [`TreeMap`](http://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) (not thread-safe) and [`ConcurrentSkipListMap`](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html) (thread-safe). – Powerlord Feb 10 '17 at 10:50
  • While both of the implementations mentioned above are both `SortedMap` and `NavigableMap`, using `NavigableMap` exposes the `descendingMap` function, which returns a view of the map sorted in the opposite direction. – Powerlord Feb 10 '17 at 10:56

1 Answers1

1

Just iterating and removing while doing so, will result in a Concurrent Modification Exception. You could temp save the item to remove and remove it later: For example:

void removePatient(String patientName) {
    Person deleteThat;
    for (Patient p : ts){
       if(p.getName().equals(patientName){
           deleteThat = p;
       }
    }
    if(deleteThat != null){
        ts.remove(deleteThat);
    }
}
danny
  • 358
  • 2
  • 14