Hab da mal was:
[code=Java] public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<String, Object>();
// 10 zufällige Elemente einfügen
Random r = new Random();
for (int i = 0; i < 10; i++) {
String s;
while (map.containsKey(s = Integer.toString(r.nextInt(20))));
map.put(s, null);
}
// 10 Elemente erneut einfügen
for (int i = 0; i < 10; i++) {
putMap(map, map.keySet().toArray(new String[0])[r.nextInt(map.size())]);
}
// Ausgeben
String[] a = map.keySet().toArray(new String[0]);
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
public static void putMap(Map<String, Object> map, String s) { // + value parameter
int idx = 0;
if (s.matches("^\\d+_\\d+$")) {
int indexOf_ = s.indexOf('_');
idx = Integer.parseInt(s.substring(indexOf_ + 1));
s = s.substring(0, indexOf_);
}
String sNew;
while (map.containsKey(sNew = s + "_" + ++idx));
map.put(sNew, null);
}[/code]
[code][0, 11, 12, 12_1, 12_2, 13, 13_1, 14, 14_1, 15, 17, 17_1, 19, 8, 8_1, 9, 9_1, 9_2, 9_3, 9_4][/code]
Das Entfernen ist aber nicht so einfach wie das Einfügen, wenn es keine "Lücken" geben soll