To begin we create a HashMap and specify Integer keys and String values. We then use put() to add 3 entries to the HashMap.
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// Create a HashMap.
HashMap<Integer, String> hash = new HashMap<>();
hash.put(100,
"one-hundred");
hash.put(1000,
"one-thousand");
hash.put(50,
"fifty");
// Use Entry type.
// ... Create an ArrayList and call addAll to add the entrySet.
ArrayList<Entry<Integer, String>> array = new ArrayList<>();
array.addAll(hash.entrySet());
// Loop over ArrayList of Entry elements.
for (Entry<Integer, String> entry : array) {
// Use each ArrayList element.
int key = entry.getKey();
String value = entry.getValue();
System.out.println(
"Key = " + key +
"; Value = " + value);
}
}
}
Key = 50; Value = fifty
Key = 100; Value = one-hundred
Key = 1000; Value = one-thousand