HashMap - Das assoziative, dynamische Array in Java

Java  (20.07.2010 20:11) Die HashMap aus Java ist vergleichbar mit einem normalen Array in PHP.
Sie ist dynamisch in der Anzahl der Einträge und ist in der Lage assoziative Feldnamen zu nutzen.
Man muss sich nur beim Index und beim Inhalt auf jeweils einen zentralen Datentyp festlegen.

Quellcode (ausblenden | aufklappen)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package javatest;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 *
 * @author sim4000
 */
public class Main {
    public static void main(String[] args) {
 
       HashMap<Integer, String> map = new HashMap<Integer, String>();
       int random = (int)(Math.random()*100);
 
       System.out.println("Zufallszahl: "+random);
       for(int i=0; i<random; i++) {
          map.put(i, Integer.toHexString(i));
       }
 
       for ( Map.Entry<Integer, String> e : map.entrySet() ) {
          System.out.println(e.getKey()+": "+e.getValue());
       }
 
    }
}