Wednesday, July 20, 2011

HashTable

import java.util.HashMap;

/**
*
*/

/**
* @author Sajjad
*
*/
class Student
{
private int id;
private String name;
private String std;

public int getId() {
return id;
}

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

@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", std=" + std + "]";
}


}
public class hashTable {

/**
* @param args
*/
public static HashMap buildMap(Student[] students) {
HashMap map = new HashMap();
for (Student s : students) map.put(s.getId(), s);
return map;
}
public static void main(String[] args) {
Student[] students = new Student[5];
students[0]=new Student(1, "abdulla", "KG1");
students[1]=new Student(2, "Mohd", "KG2");
students[2]=new Student(3, "Harry", "2");
students[3]=new Student(4, "Saud", "KG1");
students[4]=new Student(5, "John", "KG2");

// TODO Auto-generated method stub
// for (Student s : students)
//{
// System.out.println(s);
//}
HashMap myMap = new HashMap();
myMap=buildMap(students);
System.out.println(myMap);

}

}

No comments:

Post a Comment