Just copy paste below statement to your code and rename the HashMap variable from hm to your HashMap variable to print out key-value pair. ("ID =>" + key + " Name => " + value) Īs you can see in the above code, we have iterated the map with forEach 2. In Java 1.8 (Java 8) this has become lot easier by using forEach method from Aggregate operations ( Stream operations) that looks similar to iterators from Iterable Interface. iterating key and value with forEach loop entrySet().iterator() īased on the requirement, we can iterate the map in any of the above ways. getValue(): It is used for getting a key-mapped value.Īs discussed above, Map can be iterated with forEach, normal for, and while loop.getKey(): It is used for getting a key value.size(): It is used for getting map size.entrySet(): It is used to set keys and values to the map.keySet(): It is used for getting a key pair set.put(Object keyValue, Object objectValue): It inserts some value into a map.The entry provides methods to fetch key and value pair. It returned the collection view of the map. Therefore, it will be accessible by the Entry name. If you want to learn more about how to start working with Thymeleaf in Spring Boot, check out the this guide.2. Thymeleaf offers powerful features that make it ideal for modern HTML5 Java-based web development. Thymeleaf provides th:each attribute that can be used to loop through different Java collection objects like Map, List, Set and array. In this article, we looked at how iteration works in Thymeleaf templates. Here is an example that shows how you can iterate a User array object in Thymeleaf: 1 John Doe 27 Janu Conclusion The th:each attribute also accepts a Java array object as an iterable. Iterating through a Java Set collection is very much similar to a List collection as shown below: 1 John Doe 27 Janu Iterating through Array The following example demonstrates how you can loop through a Java List object by using th:each attribute in Thymeleaf: 1 John Doe 27 Janu Iterating through Set Let us look at an example that uses the th:each attribute to loop through a Java Map object: # ID First Name Last Name Email Age Created 1 100 John Doe 27 Janu Iterating through List
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |