-1

My code:

HashMap<Integer,Student> Chemistry= new HashMap<Integer,Student>();
Chemistry.put(3333,"NICK");
for (int keys : Chemistry.keySet()){
             System.out.println(keys + ":"+ Chemistry.get(keys));
 }

the output is:

3333:Student@4aa298b7
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    override toString(). – Ousmane D. Jun 10 '18 at 17:33
  • See above comments, but also why are you using a HashMap when you're clearly only using it as an ArrayList? – Mike 'Pomax' Kamermans Jun 10 '18 at 17:34
  • @Mike'Pomax'Kamermans because OP doesn't want to store 3333 elements of nothing? – Andy Turner Jun 10 '18 at 17:39
  • I'd imagine you actually have `Chemistry.put(3333, new Student("NICK"));`, or similar? You can't put a string value into that map. – Andy Turner Jun 10 '18 at 17:40
  • @AndyTurner that's an odd comment. I'm assuming the 3333 is the student number, because storing students with gaps doesn't even make sense, but unless either of us are the OP, we can't answer the why. – Mike 'Pomax' Kamermans Jun 10 '18 at 17:42
  • @Mike'Pomax'Kamermans do you expect every student to study Chemistry? – Andy Turner Jun 10 '18 at 17:44
  • What subject they take is not even remotely relevant when the code blind-iterates over the datastructure. I don't see any other code, _maybe_ OP is better off not using a HashMap. But again, neither you nor I know this. We can invent all sorts of "what ifs", none of which matter because it's not either of our code. – Mike 'Pomax' Kamermans Jun 10 '18 at 17:53
  • i use the hasmap to store the student with their id(as key) and as value i put a type-student object.the whole concept is to create a student type-object for each student,keeping info about em(name,id,courses etc) and i create a hashmap for some courses(chemistry,math etc) and i store the students who have not pass the certain course – Nikos Psilodimitrakopoylos Jun 10 '18 at 17:55
  • 1
    @Mike'Pomax'Kamermans i want to get used to using hashmaps.Im practising so i dont want the easy way – Nikos Psilodimitrakopoylos Jun 10 '18 at 18:08

1 Answers1

1

That's the default implementation on toString() you're seeing. You could override it to get something more useful. E.g.:

public class Student {
    private String name;

    // Constructors, methods, the whole nine yards...

    @Override
    public String toString() {
        return "A student called " + name;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350