In this post about java map / dictionary:
- Define a java map
- Adding new element to a map
- Remove element from a Java map
- Get element by key, get all keys
- Iterating elements of Java map
- Performance test
Video explaining java maps : java map examples
Define a java map
In java you can define new map with different types of keys and values(Primitive types like int can't be used in java map:
import java.util.HashMap;
import java.util.Map;
Map<String, String> stringMap = new HashMap<>();
Map<Integer, String> mixedMap = new HashMap<>();
Map<Integer, Integer> intMap = new HashMap<>();
you need to import the classes from java.util one by one or to import java.util.* for all.
Adding new element to a map
If you want to add new elements to a map you can use method put which takes two arguments:
- key
- value
You have to be careful with the key and value types:
Map<String, String> stringMap = new HashMap<>();
Map<Integer, String> mixedMap = new HashMap<>();
Map<Integer, Integer> intMap = new HashMap<>();
stringMap.put("1", "First element");
mixedMap.put(1, "First element");
intMap.put(1, 1);
you need to import the classes from java.util one by one or to import java.util.* for all.
Note: if you put wrong type you will get error like:
Error:(13, 23) java: incompatible types: int cannot be converted to java.lang.String
or
Error:(15, 23) java: incompatible types: java.lang.String cannot be converted to java.lang.Integer
Note: Adding new element with the same key will override the first element!
mixedMap.put(1, "First element");
mixedMap.put(1, "First element2");
mixedMap.forEach((key, value) -> System.out.printf("%d %s%n", key, value));
result:
1 First element2
Remove element from a Java map
Removing element can be done by providing a key to method remove:
Map<String, String> stringMap = new HashMap<>();
Map<Integer, String> mixedMap = new HashMap<>();
Map<Integer, Integer> intMap = new HashMap<>();
stringMap.remove("1");
mixedMap.remove(1);
intMap.remove(1);
Note: In case of missing element nothing will happen!
Get element by key, get all keys
You can use two methods:
- mixedMap.keySet() - to get a list of all keys
- mixedMap.get(key) - to get an element by key
for (Integer key : mixedMap.keySet()) {
System.out.printf("%d %s%n", key, mixedMap.get(key));
}
Iterating elements of Java map
Since java * you can use for_each to iterate over map elements:
mixedMap.put(1, "First element");
mixedMap.put(2, "First element2");
mixedMap.put(3, "First element3");
mixedMap.forEach((key, value) -> System.out.printf("%d %s%n", key, value));
result:
1 First element
2 First element2
3 First element3
prior java 8 or without for_each example:
mixedMap.put(1, "First element");
mixedMap.put(2, "First element2");
mixedMap.put(3, "First element3");
for (Integer key : mixedMap.keySet()) {
System.out.printf("%d %s%n", key, mixedMap.get(key));
}
result:
1 First element
2 First element2
3 First element3
Performance test
Several test are done in java 8 with creating a map and adding 1 million elements. Removing an random element or getting value of such:
- Integer keys - less than a second
- String keys - about 1.735100 seconds
So for most operation java maps are fast and efficient data structure when you need to have your value in a dictionary pairs - key and value.
Even writing all elements to console takes about 10 seconds.