Mal als einfaches Beispiel:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class TypA{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String toString() {
return "Ich bin ein TypA mit Wert: " + String.valueOf(value);
}
}
class TypB{
private double value;
private TypA einAndererTyp;
public TypA getEinAndererTyp() {
return einAndererTyp;
}
public void setEinAndererTyp(TypA einAndererTyp) {
this.einAndererTyp = einAndererTyp;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String toString() {
return "Ich bin ein TypB mit Wert: " + String.valueOf(value) + „/“ + einAndererTyp.toString();
}
}
public class BeispielKlasse {
public static void main(String[] args) {
TypA a1 = new TypA();
a1.setValue(12);
TypA a2 = new TypA();
a2.setValue(4711);
TypB b = new TypB();
b.setEinAndererTyp(a1);
b.setValue(22.4);
Map resultMap = new HashMap();
resultMap.put(„key1“, a1);
resultMap.put(„key2“, a2);
resultMap.put(„key3“, b);
for (Iterator iter = resultMap.keySet().iterator(); iter.hasNext()
{
Object tmpKey = iter.next();
Object tmpValue = resultMap.get(tmpKey);
System.out.println("Key: " + tmpKey.toString());
System.out.println("Value: " + tmpValue.toString());
System.out.println();
}
}
}