Created
March 24, 2015 18:13
-
-
Save joanmolinas/38ec520ea3a63de56ff0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.