Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
7-9. Iterating Over a Map
Problem You use one of the Map classes, such as HashMap or TreeMap, and you need to iterate the keys, values, or both. You also want to remove elements from the map while iterating over it. Solution There are multiple ways to iterate a Map class. The method you choose should depend on which portions of the map you need to access and whether you need to remove elements from the map while iterating. The StockPortfolio1 class is a continuation of the StockPorfolio class shown in the previous recipe. It adds three methods—summary(), alertList(), and remove(List iterating over the portfolio map: Map The following is the class. import org.java17recipes.chapter07.recipe07_07.Stock; import org.java17recipes.chapter07.recipe07_06.StockScreener; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; Chapter 7 Data SourCeS anD ColleCtionS 283 public class StockPortfolio1 implements Iterable Map public void add(Stock stock) { portfolio.put(stock.getSymbol(), stock); } public void add(List for (Stock s : stocks) { portfolio.put(s.getSymbol(), s); } } public void remove(String stock) { portfolio.remove(stock); } public void remove(List Iterator while (keyIter.hasNext()) { if (sellList.contains(keyIter.next())) { keyIter.remove(); } } } /** * Utilize for-loop to traverse Map keys and apply filter to obtain desired * stocks * @return */ public List System.out.println("==Legacy technique for filtering and collecting=="); List for (Stock stock : portfolio.values()) { Chapter 7 Data SourCeS anD ColleCtionS 284 if (!StockScreener.screen(stock.getSymbol(), StockScreener. Screen.PE, 20)) { alertList.add(stock); } } return alertList; } /** * Utilize stream and filters to obtain desired stocks * @return */ public List return portfolio.values().stream() .filter(s->!StockScreener.screen(s.getSymbol(), StockScreener.Screen.PE, 20)) .collect(Collectors.toList()); } public void summary() { System.out.println("==Legacy technique for traversing Map.Entry=="); for (Map.Entry System.out.println("Stock = " + entry.getKey() + ", Shares = " + entry.getValue().getShares()); } System.out.println("==Utilization of new foreach and lambda combination=="); portfolio.forEach((k,v)->System.out.println("Stock = " + k + ", Shares = " + v.getShares())); } Chapter 7 Data SourCeS anD ColleCtionS 285 @Override public Iterator return portfolio.values().iterator(); } public static void main(String[] args) { StockPortfolio1 myPortfolio = new StockPortfolio1(); myPortfolio.add(new Stock("ORCL", "Oracle", 500.0)); myPortfolio.add(new Stock("AAPL", "Apple", 200.0)); myPortfolio.add(new Stock("GOOG", "Google", 100.0)); myPortfolio.add(new Stock("IBM", "IBM", 50.0)); myPortfolio.add(new Stock("MCD", "McDonalds", 300.0)); // foreach loop (uses Iterator returned from iterator() method) for (Stock stock : myPortfolio) { System.out.println(stock); } myPortfolio.forEach((stock)->System.out.println(stock)); List sellList.add("IBM"); sellList.add("GOOG"); myPortfolio.remove(sellList); System.out.println("Portfolio Summary:"); myPortfolio.summary(); System.out.println("Alerts:"); for (Stock stock : myPortfolio.alertList()) { System.out.println("Alert: " + stock.getSymbol()); } } } Chapter 7 Data SourCeS anD ColleCtionS 286 The output is: 100.0 shares of GOOG (Google) 200.0 shares of AAPL (Apple) 50.0 shares of IBM (IBM) 500.0 shares of ORCL (Oracle) 300.0 shares of MCD (McDonalds) 100.0 shares of GOOG (Google) 200.0 shares of AAPL (Apple) 50.0 shares of IBM (IBM) 500.0 shares of ORCL (Oracle) 300.0 shares of MCD (McDonalds) Portfolio Summary: ==Legacy technique for traversing Map.Entry== Stock = AAPL, Shares = 200.0 Stock = ORCL, Shares = 500.0 Stock = MCD, Shares = 300.0 ==Utilization of new foreach and lambda combination== Stock = AAPL, Shares = 200.0 Stock = ORCL, Shares = 500.0 Stock = MCD, Shares = 300.0 Alerts: AAPL: PE = 6.152926256441124 ORCL: PE = 13.539727307058891 MCD: PE = 20.073565345827422 Alert: MCD 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