import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/*
* Created on 04.05.2004
*/
/**
* @author Thomas Darimont
*/
public class HashtableTest {
private HashMap map = new HashMap();
public static void main(String[] args) {
new HashtableTest().testIt();
}
/**
*
*/
private void testIt() {
Student tom = new Student("Darimont", "Thomas", 122342);
List lectureList = new ArrayList();
Lecture lecture0 = new Lecture("Programmierung");
Lecture lecture1 = new Lecture("Analysis");
Lecture lecture2 = new Lecture("Consulting");
lectureList.add(lecture0);
lectureList.add(lecture1);
lectureList.add(lecture2);
map.put(tom, lectureList);
//..........
System.out.println(findStudent(tom));
List list = (List) map.get(tom);
Object[] items = list.toArray();
for (int i = 0; i < items.length; i++) {
System.out.println(((Lecture) items[i]).getName());
}
}
public boolean findStudent(Student s) {
if (map.containsKey(s)) {
return true;
} //if
else
return false;
} //findMatrikel
class Student {
private int hashCode;
private String vorname;
private String nachname;
private int matrikelNr;
public Student(String name, String vname, int matNr) {
nachname = name;
vorname = vname;
matrikelNr = matNr;
}
public int hashCode() {
if (hashCode == 0) {
hashCode = (matrikelNr + vorname + nachname).hashCode();
}
return hashCode;
}
/**
* @return
*/
public int getMatrikelNr() {
return matrikelNr;
}
/**
* @return
*/
public String getNachname() {
return nachname;
}
/**
* @return
*/
public String getVorname() {
return vorname;
}
/**
* @param i
*/
public void setMatrikelNr(int i) {
matrikelNr = i;
}
/**
* @param string
*/
public void setNachname(String string) {
nachname = string;
}
/**
* @param string
*/
public void setVorname(String string) {
vorname = string;
}
}
class Lecture {
private String name;
public Lecture(String n) {
name = n;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
}
}