This is a simple Java project which demonstrates the various ways in which we can iterate over the keys in a HashMap. We'll look at the following five methods:
- Using the Iterator that comes in the Collection framework
- Using the forEach loop on the HashMap's keySet
- Using the forEachRemaining loop on the HashMap's iterator
- Converting the keySet into a stream and then using forEach loop
- Converting the keySet into an array, creating a stream from that array, and then using the forEach loop
There are examples and code snippets for the first methods in the code.
Once you clone this repo, cd
into the project root directory and run the following command to compile and build this maven project:
mvn clean install
Once you run this command, Maven will build the project and keep it in the /target
directory in the project root directory.
You can run the program using the command below:
java -jar target/hashmap_iteration_poc-1.0-SNAPSHOT.jar