Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
A Map object contains a collection of key/value pairs. Maps can be beneficial when you need to store an index (key) and associate it with a particular value. A Map object must not contain any duplicate keys and each key maps to exactly one value. The source code for the solution (StockPortfolio1.java) demonstrates how to add and remove entries from a map. It also contains the source listed in the solution to this recipe, demonstrating how to iterate map entries using legacy techniques and newer syntax that takes advantage of lambda expressions and streams. Chapter 7 Data SourCeS anD ColleCtionS 287 The summary() method uses a foreach loop implementation to iterate the portfolio map’s Entry set. To iterate using the legacy code, the Map entrySet() method returns a Set of Map.Entry objects. Within the loop, you then have access to the key and value for the current Map.Entry by calling the respective key() and value() methods on that entry. Use this method of iterating when you need to access both the map keys and values while iterating, and you don’t need to remove elements from the map. Looking at the newer syntax, you can see that the same iteration can be performed in a single line of code. The newer syntax utilizes the forEach() method, which was added to the Map interface in Java 8. It applies a lambda expression to each entry within the list. The lambda expression takes both the key and value as arguments and prints them out. The alertListLegacy() method uses a foreach loop implementation to iterate the values of the portfolio map. The Map values() method returns a collection of the map values; in this case, a collection of stocks. Use this method of iterating when you only need access to the map values, and you don’t need to remove elements from the list. Similarly, if you only need access to the map keys (again, without the need to remove elements), you can iterate using the keySet() method. for (String symbol : portfolio.keySet()) { ... } If you also need to access the map value while iterating using the key set, avoid the following, as it is very inefficient. Instead, use the method of iteration shown in the summary() method. for (String symbol : portfolio.keySet()) { Stock stock = portfolio.get(symbol); ... } Looking at the alertList() method in the solution, you can see that the same iteration can be performed with much less work using a combination of streams, filters, and collectors. In alertList(), a stream is generated, and then a filter, in the form of a lambda expression, is applied to that stream. Finally, a collector is applied to the filter, creating a List The remove(List stocks to be removed from the portfolio. This method iterates over the portfolio map keys using the keySet() iterator, removing the current map entry if one of the stocks specified Chapter 7 Data SourCeS anD ColleCtionS 288 for removal. Notice that the map element is removed through the iterator’s remove() method. This is possible because the map backs the key set, so changes made through the key set’s iterator are reflected in the map. You could also iterate the portfolio map using its values() iterator. Iterator while (valueIter.hasNext()) { if (sellList.contains(valueIter.next().getSymbol())) { valueIter.remove(); } } As with the key set, the values collection is backed by the map, so calling remove() through the values iterator results in removing the current entry from the portfolio map. In summary, if you need to remove elements from a map while iterating over the map, iterate using one of the map’s collection iterators and remove map elements through the iterator, as shown in the remove(List safe way to remove map elements during iteration. Otherwise, if you don’t need to remove map elements, you can use a foreach loop and one of the methods of iteration shown in the solution to this recipe. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling