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 Retrieving Items by Index Key 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="UsingSecondaries.html" title="Chapter 3. 		Using Secondary Indices 	" /> 13 <link rel="previous" href="indexedcollections.html" title=" 		Creating Indexed Collections 	" /> 14 <link rel="next" href="Entity.html" title="Chapter 4. Using Entity Classes	 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Retrieving Items by Index Key 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="indexedcollections.html">Prev</a> </td> 26 <th width="60%" align="center">Chapter 3. 27 Using Secondary Indices 28 </th> 29 <td width="20%" align="right"> <a accesskey="n" href="Entity.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="retrievingbyindexkey"></a> 39 Retrieving Items by Index Key 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 Retrieving information via database index keys can be 47 accomplished using the standard Java collections API, using a 48 collection created from a 49 50 <a href="/java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a> 51 52 rather than a 53 54 <span> 55 <a href="/java/com/sleepycat/db/Database.html" target="_top">Database</a>. 56 </span> 57 However, the standard Java API does not support <span class="emphasis"><em>duplicate keys</em></span>: more 58 than one element in a collection having the same key. All three 59 indices created in the prior section have duplicate keys because of 60 the nature of the city, part number and supplier number index keys. 61 More than one supplier may be in the same city, and more than one 62 shipment may have the same part number or supplier number. This 63 section describes how to use extended methods for stored 64 collections to return all values for a given key. 65</p> 66 <p> 67 Using the standard Java collections API, the 68 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#get" target="_top">Map.get</a> 69 70 method for a stored collection with duplicate keys will return only 71 the first value for a given key. To obtain all values for a given 72 key, the 73 <a href="/java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a> 74 75 method may be called. This returns a 76 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a> 77 78 of values for the given key. If duplicate keys are not allowed, the 79 returned collection will have at most one value. If the key is not 80 present in the map, an empty collection is returned. 81</p> 82 <p> 83 The <tt class="classname">Sample</tt> class is extended to retrieve duplicates for 84 specific index keys that are present in the database. 85</p> 86 <a id="index_sampleviewsprintdatabase"></a> 87 <pre class="programlisting">import java.util.Iterator; 88... 89public class Sample 90{ 91 ... 92 private SampleViews views; 93 ... 94 private class PrintDatabase implements TransactionWorker 95 { 96 public void doWork() 97 throws Exception 98 { 99 printEntries("Parts", 100 views.getPartEntrySet().iterator()); 101 printEntries("Suppliers", 102 views.getSupplierEntrySet().iterator()); 103<b class="userinput"><tt> printValues("Suppliers for City Paris", 104 views.getSupplierByCityMap().duplicates( 105 "Paris").iterator());</tt></b> 106 printEntries("Shipments", 107 views.getShipmentEntrySet().iterator()); 108<b class="userinput"><tt> printValues("Shipments for Part P1", 109 views.getShipmentByPartMap().duplicates( 110 new PartKey("P1")).iterator()); 111 printValues("Shipments for Supplier S1", 112 views.getShipmentBySupplierMap().duplicates( 113 new 114 SupplierKey("S1")).iterator());</tt></b> 115 } 116 } 117 118<b class="userinput"><tt> private void printValues(String label, Iterator iterator) 119 { 120 System.out.println("\n--- " + label + " ---"); 121 while (iterator.hasNext()) 122 { 123 System.out.println(iterator.next().toString()); 124 } 125 } </tt></b> 126 ... 127} </pre> 128 <p> 129 The 130 <a href="/java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a> 131 132 method is called passing the desired key. The returned value is a 133 standard Java 134 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a> 135 136 containing the values for the specified key. A standard Java 137 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Iterator.html" target="_top">Iterator</a> 138 139 is then obtained for this collection and all values returned by 140 that iterator are printed. 141</p> 142 <p> 143 Another technique for retrieving duplicates is to use the 144 collection returned by 145 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#entrySet" target="_top">Map.entrySet</a>. 146 When duplicate keys are present, a 147 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a> 148 149 object will be present in this collection for each duplicate. This 150 collection can then be iterated or a subset can be created from it, 151 all using the standard Java collection API. 152</p> 153 <p> 154 Note that we did not discuss how duplicates keys can be 155 explicitly added or removed in a collection. For index keys, the 156 addition and deletion of duplicate keys happens automatically when 157 records containing the index key are added, updated, or 158 removed. 159</p> 160 <p> 161 While not shown in the example program, it is also possible to 162 create a store with duplicate keys in the same way as an index with 163 duplicate keys — by calling 164 <tt class="methodname">DatabaseConfig.setSortedDuplicates()</tt> method. In that case, 165 calling 166 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#put" target="_top">Map.put</a> 167 168 will add duplicate keys. To remove all duplicate keys, call 169 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#remove" target="_top">Map.remove</a>. 170 To remove a specific duplicate key, call 171 <a href="/java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a> 172 173 and then call 174 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#remove" target="_top">Collection.remove</a> 175 176 using the returned collection. Duplicate 177 values may also be added to this collection using 178 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add" target="_top"> Collection.add</a>. 179</p> 180 <p> 181 The output of the example program is shown below. 182</p> 183 <pre class="programlisting">Adding Suppliers 184Adding Parts 185Adding Shipments 186 187--- Parts --- 188PartKey: number=P1 189PartData: name=Nut color=Red weight=[12.0 grams] city=London 190PartKey: number=P2 191PartData: name=Bolt color=Green weight=[17.0 grams] city=Paris 192PartKey: number=P3 193PartData: name=Screw color=Blue weight=[17.0 grams] city=Rome 194PartKey: number=P4 195PartData: name=Screw color=Red weight=[14.0 grams] city=London 196PartKey: number=P5 197PartData: name=Cam color=Blue weight=[12.0 grams] city=Paris 198PartKey: number=P6 199PartData: name=Cog color=Red weight=[19.0 grams] city=London 200 201--- Suppliers --- 202SupplierKey: number=S1 203SupplierData: name=Smith status=20 city=London 204SupplierKey: number=S2 205SupplierData: name=Jones status=10 city=Paris 206SupplierKey: number=S3 207SupplierData: name=Blake status=30 city=Paris 208SupplierKey: number=S4 209SupplierData: name=Clark status=20 city=London 210SupplierKey: number=S5 211SupplierData: name=Adams status=30 city=Athens 212 213<b class="userinput"><tt>--- Suppliers for City Paris --- 214SupplierData: name=Jones status=10 city=Paris 215SupplierData: name=Blake status=30 city=Paris</tt></b> 216 217--- Shipments --- 218ShipmentKey: supplier=S1 part=P1 219ShipmentData: quantity=300 220ShipmentKey: supplier=S2 part=P1 221ShipmentData: quantity=300 222ShipmentKey: supplier=S1 part=P2 223ShipmentData: quantity=200 224ShipmentKey: supplier=S2 part=P2 225ShipmentData: quantity=400 226ShipmentKey: supplier=S3 part=P2 227ShipmentData: quantity=200 228ShipmentKey: supplier=S4 part=P2 229ShipmentData: quantity=200 230ShipmentKey: supplier=S1 part=P3 231ShipmentData: quantity=400 232ShipmentKey: supplier=S1 part=P4 233ShipmentData: quantity=200 234ShipmentKey: supplier=S4 part=P4 235ShipmentData: quantity=300 236ShipmentKey: supplier=S1 part=P5 237ShipmentData: quantity=100 238ShipmentKey: supplier=S4 part=P5 239ShipmentData: quantity=400 240ShipmentKey: supplier=S1 part=P6 241ShipmentData: quantity=100 <b class="userinput"><tt> 242 243--- Shipments for Part P1 --- 244ShipmentData: quantity=300 245ShipmentData: quantity=300 246 247--- Shipments for Supplier S1 --- 248ShipmentData: quantity=300 249ShipmentData: quantity=200 250ShipmentData: quantity=400 251ShipmentData: quantity=200 252ShipmentData: quantity=100 253ShipmentData: quantity=100</tt></b> </pre> 254 </div> 255 <div class="navfooter"> 256 <hr /> 257 <table width="100%" summary="Navigation footer"> 258 <tr> 259 <td width="40%" align="left"><a accesskey="p" href="indexedcollections.html">Prev</a> </td> 260 <td width="20%" align="center"> 261 <a accesskey="u" href="UsingSecondaries.html">Up</a> 262 </td> 263 <td width="40%" align="right"> <a accesskey="n" href="Entity.html">Next</a></td> 264 </tr> 265 <tr> 266 <td width="40%" align="left" valign="top"> 267 Creating Indexed Collections 268 </td> 269 <td width="20%" align="center"> 270 <a accesskey="h" href="index.html">Home</a> 271 </td> 272 <td width="40%" align="right" valign="top"> Chapter 4. 273 Using Entity Classes 274 </td> 275 </tr> 276 </table> 277 </div> 278 </body> 279</html> 280