import
java.util.Hashtable;
import
java.util.Scanner;
public
class
HashtableExample2 {
public
static
void
main(String a[])
{
Hashtable<String, Student> list
=
new
Hashtable<String, Student>();
list.put(
"Ram"
,
new
Student(
1
,
"Ram"
,
"8907654321"
));
list.put(
"Sita"
,
new
Student(
2
,
"Sita"
,
"9809876543"
));
list.put(
"Mohan"
,
new
Student(
3
,
"Mohan"
,
"9098765421"
));
list.put(
"Soham"
,
new
Student(
4
,
"Soham"
,
"7898790678"
));
list.put(
"Lakhan"
,
new
Student(
5
,
"Lakhan"
,
"7890567845"
));
System.out.println(
"Traversing the hash table"
);
for
(Student i : list.values()) {
System.out.println(i);
}
System.out.println();
System.out.println(
"Search the student by student id"
);
System.out.println();
System.out.println(
"Enter the student id : "
);
int
temp =
3
;
System.out.println(
"3"
);
for
(Student s : list.values()) {
if
(s.getRollno() == temp) {
System.out.println(
"Student is : "
+ s);
System.exit(
0
);
}
}
System.out.println(
"Student not found.."
);
}
}
class
Student {
private
int
rollno;
private
String name;
private
String mobno;
public
Student(
int
rollno, String name, String mobno)
{
super
();
this
.rollno = rollno;
this
.name = name;
this
.mobno = mobno;
}
public
int
getRollno() {
return
rollno; }
public
void
setRollno(
int
rollno)
{
this
.rollno = rollno;
}
public
String getName() {
return
name; }
public
void
setName(String name) {
this
.name = name; }
public
String getMobno() {
return
mobno; }
public
void
setMobno(String mobno)
{
this
.mobno = mobno;
}
@Override
public
int
hashCode()
{
return
this
.getRollno();
}
public
boolean
equals(Object o)
{
if
(o
instanceof
Student) {
return
(
this
.rollno) == (((Student)o).rollno);
}
return
false
;
}
@Override
public
String toString()
{
return
"Rollno="
+ rollno +
", name="
+ name
+
", mobno="
+ mobno;
}
}