Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created March 24, 2015 18:13
Show Gist options
  • Save joanmolinas/38ec520ea3a63de56ff0 to your computer and use it in GitHub Desktop.
Save joanmolinas/38ec520ea3a63de56ff0 to your computer and use it in GitHub Desktop.
//Student Class
class Student {
String name;
Student(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}
//HashMap
//HashMap is implemented as a hash table, and there is no ordering on keys or values.
HashMap<Student, Integer> students = new HashMap<Student, Integer>();
Student s1 = new Student("John");
Student s2 = new Student("Mark");
Student s3 = new Student("Daniel");
Student s4 = new Student("Joseph");
students.put(s1, 10);
students.put(s2, 4);
students.put(s3, 8);
students.put(s4, 5);
for (Entry<Student, Integer> entry : students.entrySet()) {
System.out.println(entry.getKey().toString() + " - " + entry.getValue());
}
}
@suhailumalkar
Copy link

my student class has id, name and address. i would like to take Student id as the key for hashmap and other details of student as the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment