map interface java code example
Example 1: map interation in java
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
Example 2: java create map
Map <Integer, Point2D.Double> hm = new HashMap<Integer, Point2D>();
hm.put(1, new Point2D.Double(50, 50));
Example 3: map java
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hm = new HashMap<>();
hm.put(10, "1");
hm.put(20, "2");
hm.put(30, "3");
hm.put(40, "4");
hm.put(50, "5");
hm.put(50, "6");
System.out.println("Parcours de l'objet HashMap : ");
Set<Entry<Integer, String>> setHm = hm.entrySet();
Iterator<Entry<Integer, String>> it = setHm.iterator();
while(it.hasNext()){
Entry<Integer, String> e = it.next();
System.out.println(e.getKey() + " : " + e.getValue());
}
System.out.println("Valeur pour la clé 8 : " + hm.get(8));
Map<Integer, String> lhm = new LinkedHashMap<>();
lhm.put(10, "1");
lhm.put(20, "2");
lhm.put(30, "3");
lhm.put(40, "4");
lhm.put(50, "5");
System.out.println("Parcours de l'objet LinkedHashMap : ");
Set<Entry<Integer, String>> setLhm = lhm.entrySet();
Iterator<Entry<Integer, String>> it2 = setLhm.iterator();
while(it2.hasNext()){
Entry<Integer, String> e = it2.next();
System.out.println(e.getKey() + " : " + e.getValue());
}
}
}