Back to Blog
Java collections6/2/2023 ![]() For example, while List and Deque define an encounter order, their common supertype is Collection, which does not. The lack of a collection type representing a sequence of elements with a defined encounter order has been a repeated source of problems and complaints. In response, a proposal has been proposed to introduce new interfaces to represent collections with a defined encounter order and provide uniform APIs for accessing their first and last elements and processing them in reverse order. Unfortunately, support for encounter order is currently spread across the type hierarchy, making expressing certain practical concepts in APIs difficult. Java's collections framework has long needed a collection type that represents a sequence of elements with a defined encounter order and a uniform set of operations that apply across such collections. It proposes introducing "a new family of interfaces that represent the concept of a collection whose elements are arranged in a well-defined sequence or ordering, as a structural property of the collection." This is motivated by the need for a well-defined ordering and a uniform set of operations within the Collections Framework. ![]() The later is always better than the former, that’s how the language evolves over time.Nowadays, the forEach method has been being adopted and preferred as it is deadly simple, convenient and flexible.JEP 431: Sequenced Collections has been promoted from Candidate to Proposed to Target status for JDK 21. The code is highly compact, more readable and more flexible.Similarly, we can use the forEach method to iterate over the Set above like this: numbers.forEach(number -> (number)) Or even more compact: numbers.forEach(System.out::println) And code to iterate over the Map above using the forEach method: mapAscii.forEach((key, value) -> (key + " -> " + value)) WOW! Incredible simple, right?So far I have walked you through the four methods for iterating over collections in the Java programming language: classic for loop, iterator, enhanced for loop and for each. Here’s the code that iterates over the listNames collection above: for (int i = 0 i < listNames.size() i++) Then we can pass this code into the forEach() method like this: listNames.forEach(StudentHelper::process) As you can see, this is the power of using Lambda expressions with the internal iteration method. The Classic For Loop:This iteration method is very familiar in programming in which a counter variable runs from the first element to the last one in the collection. Note that the diamond operator used in the right side of the assignment: ArrayList() This syntax can be used from Java 7, which allows us to declare generics collections in a more compact way, as the compiler can infer the parameter type in the right side from the left side (thus the so-called type inference).Now, let’s look at each method in details. ListNames.add("Kim") This list contains names of all students in a class. Such kinds of operations are very common in programming.The Java programming language provides four methods for iterating over collections, including for loops, iterator and forEach (since Java 8).Before going to each kind of iteration, suppose that we have a List collection as follows: List listNames = new ArrayList() Or you may want to traverse through a list of numbers in order to calculate the sum and average. Basically, an iteration takes elements from a collection one after another, from the first element to the last one.For example, you may want to go through all students in a class to print their names or to find who have high scores in the recent exam. ![]() Iteration is one of the basic operations carried on a collection.
0 Comments
Read More
Leave a Reply. |