Skip to content

How to access hashmap object with class object as key

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.

Answer

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