entrySet: It is a set of key-value pair in the Map.values: It is the collection of values contained in the Map.keySet: It is the set of keys contained in the Map.The collection views method allows a map to be viewed as a Collection in the following ways: Iterator is fail-fast whereas Enumeration is not fail-fast.The Iterator can traverse legacy and non-legacy elements whereas Enumeration can traverse only legacy elements.The difference between iterator and Enumeration is: It takes the place of Enumeration in Java Collections Framework. IteratorĪn iterator is an interface used for iterate over a collection. Hence, a Map is an interface which does not extend the Collections interface. There are following types of maps in Java:Ī map is not a Collection but still, consider under the Collections framework. ![]() All maps in Java implements Map interface. Remember that we cannot iterate over map directly using iterators, because Map interface is not the part of Collection. If you want to learn more about how to start working with Thymeleaf in Spring Boot, check out the this guide.In Java, iteration over Map can be done in various ways. 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 last- Check if the current iteration is the last one.first- Check if the current iteration is the first one.even/odd- Check if the current iteration index is even or odd.size- The total amount of elements in the iterated variable.count- The number of elements processed so far.index- The current iteration index, starting with 0.For example, you can pass another local variable next to iter to get the iteration statistics: Īs you can see above, stats is the status variable that contains the following properties: Thymeleaf also provides a way to keep track of the iteration process by using the status variable in th:each statement. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |