-2

I want to make a program that updates the hashmap depending on the user input commands. How can I remove/update specific element of a hashmap by passing id variable of Student class as user input? This is what I got so far:

class Student{
String name;
String secondname;
int id;
public Student(String name,String secondname,int id){
    this.id = id;
    this.name = name;
    this.secondname=secondname;
}

public int getId(){
    return this.id;
}

@Override
public String toString() {
    return "Second Name: "+ this.secondname+ " Name: "+ this.name+ " ID: "+ this.id;
}

@Override
public boolean equals(Object o) {
    if(this==o){
        return true;
    }
    if (o==null){
        return false;
    }
    if(getClass() != o.getClass()){
        return false;
    }
    Student obj = (Student) o;
    if (secondname == null) {
        if(obj.secondname!= null){
            return false;
        }
    }
    else if(!secondname.equals(obj.secondname)){
        return false;
    }
    if(name==null){
        if(obj.name!=null){
            return false;
        }
    }
    else if(!name.equals(obj.name)){
        return false;
    }
    if(getId()==0){
        if(obj.getId()!=0){
            return false;
        }
    }
    else if (getId()!=obj.getId()){
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result= prime*result+id;
    return result;
}



public class Main {

public static void main(String[] args) {
    HashMap<Student,String> studentmap = new HashMap<>();
    Student myname = new Student("Name","SecondName",1234);
    Student mrx = new Student("Mr","X",2077);
    Student msx = new Student("Ms","X",1111);
    studentmap.put(myname,"A");
    studentmap.put(mrx,"C");
    studentmap.put(msx,"B");
    while (true){
        Scanner scan = new Scanner(System.in);
        String x= scan.nextLine();
        if (x.equals("add")){
            System.out.println("Who do you want to add? ");
            String y= scan.nextLine();
            String [] splitted = y.split("\\s+");
            studentmap.put(new Student(splitted[0],splitted[1],Integer.parseInt(splitted[2])),splitted[3]);
        }
        if(x.equals("remove")){
            System.out.println("Who do you want to remove?");
            String z= scan.nextLine();
            int theid = Integer.parseint(z);
            studentmap.remove(theid); // adding and printing works but this is what I have problem with
        }
        //if (x.equals("update")){
            //String e= scan.nextLine();
            //String [] splitted = e.split("\\s+");
            //int theid = Integer.parseint(splited[0])
            //studentmap.replace(theid,splitted[1]);
        //}
        if(x.equals("print")){
            studenci.entrySet().forEach(entry->{
                System.out.println(entry.getKey() + " Grade: " + entry.getValue());
            });
        }
        if (x.equals("end")){
            break;
        }
    }
}

The way I want this program to work is to make the user type a command like "delete", then make him type ID ex."1234" and then remove a hash map object whose Key's ID is 1234.

EDIT: My assignment roughly translated to english: Make a program using a map which keys are Student class objects (with fields: name ,secondname, id ), and the values are grades. Program should allow the user to add, delete, update the grade and print the students list. In case of deleting and updating look up the object by ID.

  • 2
    Whatever the values A, B, and C represent, they would probably be better off stored in the Student object itself, then make your map of `` so you can look up the whole Student object by id. – Bill the Lizard Dec 04 '20 at 21:27
  • The point is that Student has to be hashmaps' key because that's what my school assignment wants me to do. A B C are just grades that I have to pass as hashmap value – Eryk Krygier Dec 04 '20 at 21:43
  • Be careful when you use a potentially mutable object as key into a `HashMap`. Unexpected behaviour can be exhibited when a hash map key is mutated. See also [Are mutable HashMap keys a dangerous practice?](https://stackoverflow.com/questions/7842049/are-mutable-hashmap-keys-a-dangerous-practice) Fortunately, the `Student` class doesn't have any public mutator methods at the moment, but if these are ever added, for example to change a student's name, then this should be taken into account. – MC Emperor Dec 04 '20 at 22:20

2 Answers2

0

It'd make more sense to change:

HashMap<Student,String> studentmap = new HashMap<>();
Student myname = new Student("Name","SecondName",1234);
Student mrx = new Student("Mr","X",2077);
Student msx = new Student("Ms","X",1111);
studentmap.put(myname,"A");
studentmap.put(mrx,"C");
studentmap.put(msx,"B");

Into:

HashMap<Integer,Student> studentmap = new HashMap<>();
Student myname = new Student("Name","SecondName",1234);
Student mrx = new Student("Mr","X",2077);
Student msx = new Student("Ms","X",1111);
studentmap.put( yname.getId(),myname);
studentmap.put(mrx.getId(),mrx);
studentmap.put(msx.getId(),msx);

Then when someone types 'remove', followed by the Id, you can delete like you want/wrote:

studentmap.remove(theid); // remove student 'myname' if 1234
TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6
0

You have to "find" the key from the Map<Student,String> first, which matches the id you have. After that you can use it to remove the entry from the Map<Student,String>. The code might look like this:

Student s = null;
for(Student k: studentmap.keySet()) {
    if (k.getId() == theid) {
        s = k;
        break;
    }
}

This will find you the key in the Map. After that you can remove the entry:

if (s != null) {
    studentmap.remove(s);
}
Progman
  • 16,827
  • 6
  • 33
  • 48