xxxxxxxxxx
import java.util.Map;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Declare a map
Map<String, Integer> map = new HashMap<>();
// Add key-value pairs to the map
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// Access values using keys
System.out.println(map.get("apple")); // Output: 1
System.out.println(map.get("banana")); // Output: 2
System.out.println(map.get("orange")); // Output: 3
}
}
xxxxxxxxxx
Map< String,Integer> hm =
new HashMap< String,Integer>();
hm.put("a", new Integer(100));
hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));
xxxxxxxxxx
MAP : is a (key-value format)
and keys are always unique,
and value can be duplicated.
- HashTable don't have null key, sychronized(thread-safe)
- LinkedHashMap can have null key, keeps order
- HasHMap can have null key, order is not guaranteed
- TreeMap doesn't have null key and keys are sorted