1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title> 7 Using Entities with Collections 8 </title> 9 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 10 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 11 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 12 <link rel="up" href="Entity.html" title="Chapter��4.�� Using Entity Classes	 	" /> 13 <link rel="previous" href="collectionswithentities.html" title=" 		Creating Collections with Entity Bindings 	" /> 14 <link rel="next" href="Tuple.html" title="Chapter��5.�� 		Using Tuples 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Using Entities with Collections 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="collectionswithentities.html">Prev</a>��</td> 26 <th width="60%" align="center">Chapter��4.�� 27 Using Entity Classes 28 </th> 29 <td width="20%" align="right">��<a accesskey="n" href="Tuple.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="entitieswithcollections"></a> 39 Using Entities with Collections 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 In this example entity objects, rather than key and value 47 objects, are used for adding and enumerating the records in a 48 collection. Because fewer classes and objects are involved, adding 49 and enumerating is done more conveniently and more simply than in 50 the prior examples. 51</p> 52 <p> 53 For adding and iterating entities, the collection of entities 54 returned by 55 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a> 56 57 is used. In general, when using an entity binding, all Java 58 collection methods that are passed or returned a value object will 59 be passed or returned an entity object instead. 60</p> 61 <p> 62 The <tt class="classname">Sample</tt> class has been changed in this example to add 63 objects using the 64 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html#add" target="_top">Set.add</a> 65 66 method rather than the 67 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#put" target="_top">Map.put</a> 68 69 method that was used in the prior examples. Entity objects are 70 constructed and passed to 71 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html#add" target="_top">Set.add</a>. 72</p> 73 <a id="entity_addsuppliers"></a> 74 <pre class="programlisting">import java.util.Set; 75... 76public class Sample 77{ 78 ... 79 private void addSuppliers() 80 { 81<b class="userinput"><tt> Set suppliers = views.getSupplierSet(); 82 if (suppliers.isEmpty()) 83 { 84 System.out.println("Adding Suppliers"); 85 suppliers.add(new Supplier("S1", "Smith", 20, "London")); 86 suppliers.add(new Supplier("S2", "Jones", 10, "Paris")); 87 suppliers.add(new Supplier("S3", "Blake", 30, "Paris")); 88 suppliers.add(new Supplier("S4", "Clark", 20, "London")); 89 suppliers.add(new Supplier("S5", "Adams", 30, "Athens")); 90 }</tt></b> 91 } 92 93 private void addParts() 94 { 95<b class="userinput"><tt> Set parts = views.getPartSet(); 96 if (parts.isEmpty()) 97 { 98 System.out.println("Adding Parts"); 99 parts.add(new Part("P1", "Nut", "Red", 100 new Weight(12.0, Weight.GRAMS), "London")); 101 parts.add(new Part("P2", "Bolt", "Green", 102 new Weight(17.0, Weight.GRAMS), "Paris")); 103 parts.add(new Part("P3", "Screw", "Blue", 104 new Weight(17.0, Weight.GRAMS), "Rome")); 105 parts.add(new Part("P4", "Screw", "Red", 106 new Weight(14.0, Weight.GRAMS), "London")); 107 parts.add(new Part("P5", "Cam", "Blue", 108 new Weight(12.0, Weight.GRAMS), "Paris")); 109 parts.add(new Part("P6", "Cog", "Red", 110 new Weight(19.0, Weight.GRAMS), "London")); 111 }</tt></b> 112 } 113 114 private void addShipments() 115 { 116<b class="userinput"><tt> Set shipments = views.getShipmentSet(); 117 if (shipments.isEmpty()) 118 { 119 System.out.println("Adding Shipments"); 120 shipments.add(new Shipment("P1", "S1", 300)); 121 shipments.add(new Shipment("P2", "S1", 200)); 122 shipments.add(new Shipment("P3", "S1", 400)); 123 shipments.add(new Shipment("P4", "S1", 200)); 124 shipments.add(new Shipment("P5", "S1", 100)); 125 shipments.add(new Shipment("P6", "S1", 100)); 126 shipments.add(new Shipment("P1", "S2", 300)); 127 shipments.add(new Shipment("P2", "S2", 400)); 128 shipments.add(new Shipment("P2", "S3", 200)); 129 shipments.add(new Shipment("P2", "S4", 200)); 130 shipments.add(new Shipment("P4", "S4", 300)); 131 shipments.add(new Shipment("P5", "S4", 400)); 132 }</tt></b> 133 } </pre> 134 <p> 135 Instead of printing the key/value pairs by iterating over the 136 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#entrySet" target="_top">Map.entrySet</a> 137 138 as done in the prior example, this example 139 iterates over the entities in the 140 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a> 141 142 collection. 143</p> 144 <a id="entity_printdatabase"></a> 145 <pre class="programlisting">import java.util.Iterator; 146import java.util.Set; 147... 148public class Sample 149{ 150 ... 151 private class PrintDatabase implements TransactionWorker 152 { 153 public void doWork() 154 throws Exception 155 { 156<b class="userinput"><tt> printValues("Parts", 157 views.getPartSet().iterator()); 158 printValues("Suppliers", 159 views.getSupplierSet().iterator());</tt></b> 160 printValues("Suppliers for City Paris", 161 views.getSupplierByCityMap().duplicates( 162 "Paris").iterator()); 163<b class="userinput"><tt> printValues("Shipments", 164 views.getShipmentSet().iterator());</tt></b> 165 printValues("Shipments for Part P1", 166 views.getShipmentByPartMap().duplicates( 167 new PartKey("P1")).iterator()); 168 printValues("Shipments for Supplier S1", 169 views.getShipmentBySupplierMap().duplicates( 170 new SupplierKey("S1")).iterator()); 171 } 172 } 173 ... 174} </pre> 175 <p> 176 The output of the example program is shown below. 177</p> 178 <pre class="programlisting">Adding Suppliers 179Adding Parts 180Adding Shipments 181 182--- Parts --- 183Part: number=P1 name=Nut color=Red weight=[12.0 grams] city=London 184Part: number=P2 name=Bolt color=Green weight=[17.0 grams] city=Paris 185Part: number=P3 name=Screw color=Blue weight=[17.0 grams] city=Rome 186Part: number=P4 name=Screw color=Red weight=[14.0 grams] city=London 187Part: number=P5 name=Cam color=Blue weight=[12.0 grams] city=Paris 188Part: number=P6 name=Cog color=Red weight=[19.0 grams] city=London 189 190--- Suppliers --- 191Supplier: number=S1 name=Smith status=20 city=London 192Supplier: number=S2 name=Jones status=10 city=Paris 193Supplier: number=S3 name=Blake status=30 city=Paris 194Supplier: number=S4 name=Clark status=20 city=London 195Supplier: number=S5 name=Adams status=30 city=Athens 196 197--- Suppliers for City Paris --- 198Supplier: number=S2 name=Jones status=10 city=Paris 199Supplier: number=S3 name=Blake status=30 city=Paris 200 201--- Shipments --- 202Shipment: part=P1 supplier=S1 quantity=300 203Shipment: part=P1 supplier=S2 quantity=300 204Shipment: part=P2 supplier=S1 quantity=200 205Shipment: part=P2 supplier=S2 quantity=400 206Shipment: part=P2 supplier=S3 quantity=200 207Shipment: part=P2 supplier=S4 quantity=200 208Shipment: part=P3 supplier=S1 quantity=400 209Shipment: part=P4 supplier=S1 quantity=200 210Shipment: part=P4 supplier=S4 quantity=300 211Shipment: part=P5 supplier=S1 quantity=100 212Shipment: part=P5 supplier=S4 quantity=400 213Shipment: part=P6 supplier=S1 quantity=100 214 215--- Shipments for Part P1 --- 216Shipment: part=P1 supplier=S1 quantity=300 217Shipment: part=P1 supplier=S2 quantity=300 218 219--- Shipments for Supplier S1 --- 220Shipment: part=P1 supplier=S1 quantity=300 221Shipment: part=P2 supplier=S1 quantity=200 222Shipment: part=P3 supplier=S1 quantity=400 223Shipment: part=P4 supplier=S1 quantity=200 224Shipment: part=P5 supplier=S1 quantity=100 225Shipment: part=P6 supplier=S1 quantity=100 </pre> 226 </div> 227 <div class="navfooter"> 228 <hr /> 229 <table width="100%" summary="Navigation footer"> 230 <tr> 231 <td width="40%" align="left"><a accesskey="p" href="collectionswithentities.html">Prev</a>��</td> 232 <td width="20%" align="center"> 233 <a accesskey="u" href="Entity.html">Up</a> 234 </td> 235 <td width="40%" align="right">��<a accesskey="n" href="Tuple.html">Next</a></td> 236 </tr> 237 <tr> 238 <td width="40%" align="left" valign="top"> 239 Creating Collections with Entity Bindings 240 ��</td> 241 <td width="20%" align="center"> 242 <a accesskey="h" href="index.html">Home</a> 243 </td> 244 <td width="40%" align="right" valign="top">��Chapter��5.�� 245 Using Tuples 246 </td> 247 </tr> 248 </table> 249 </div> 250 </body> 251</html> 252