Java 17 Recipes
-7. Making Your Objects Iterable
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
7-7. Making Your Objects Iterable
Problem You have created a custom collection–based class that wraps (instead of extends) the underlying collection type. Without exposing the internal implementation details of your class, you want objects of your class to become iterable, especially with the use of a foreach statement. Solution Have your class extend the Iterable collection to be iterated. Implement the iterator() method to return the Iterator object from this collection. The example for this recipe is the StockPortfolio class. Internally, StockPortfolio manages a collection of Stock objects. We want users of our class to treat StockPortfolio objects as iterable objects using a foreach statement. The StockPortfolio class follows. Chapter 7 Data SourCeS anD ColleCtionS 273 public class StockPortfolio 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); } } @Override public Iterator return portfolio.values().iterator(); } public static void main(String[] args) { StockPortfolio myPortfolio = new StockPortfolio(); 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) System.out.println("====Print using legacy for-each loop===="); for (Stock stock : myPortfolio) { System.out.println(stock); } System.out.println("====Print using Java 8 foreach implementation===="); myPortfolio.forEach(s->System.out.println(s)); } } Chapter 7 Data SourCeS anD ColleCtionS 274 The following code is that of the Stock class. public class Stock { private String symbol; private String name; private double shares; public Stock(String symbol, String name, double shares) { this.symbol = symbol; this.name = name; this.shares = shares; } public String getSymbol() { return symbol; } public String getName() { return name; } public double getShares() { return shares; } public String toString() { return shares + " shares of " + symbol + " (" + name + ")"; } } The main() method creates a StockPortfolio and then calls the add() method to add several stocks to the portfolio. Both variations of the foreach loop (legacy and forEach implementation) then loop over and print all the stocks in the portfolio. Running the StockPortfolio class results in the following output. 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) Chapter 7 Data SourCeS anD ColleCtionS 275 Note the order of the lines in the output may differ when you run the StockPortfolio class in your environment because the underlying implementation uses a hashMap the order of the elements stored in the map, and this extends to its iterators. if you wanted the iterator to return elements sorted by the stock symbol, you could use one of the sorted collections, such as treeMap hashMap. 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