The Common Java Cookbook
Total Page:16
File Type:pdf, Size:1020Kb
June 2009 July 2009 Edition 0.9 Edition 0.19 The Common Java Cookbook Tim O’Brien discursive.com/books/cjcook A Discursive Open Book Copyright and License ....................................................................................................................................xx 1. Creative Commons BY-NC-ND 3.0 US License ...................................................................................xx Foreword: 0.19 ............................................................................................................................................xxiv Preface .........................................................................................................................................................xxv 1. What's Inside ....................................................................................................................................xxv 2. Conventions Used in This Book ......................................................................................................xxviii 3. What You'll Need ............................................................................................................................xxix 4. Book Examples ................................................................................................................................xxix 5. The Apache Commons Community ...................................................................................................xxix 6. The Apache Software License ............................................................................................................xxx 7. We'd Like to Hear from You .............................................................................................................xxxi 1. Supplements to the Java 2 Platform ................................................................................................................1 1.1. Introduction .......................................................................................................................................1 1.1. Depending on Commons Lang ............................................................................................................1 1.1.1. Problem ..................................................................................................................................1 1.1.2. Solution ..................................................................................................................................1 1.1.3. Discussion ..............................................................................................................................2 1.4. Automating the Generation of toString( ) Content ................................................................................2 1.4.1. Problem ..................................................................................................................................2 1.4.2. Solution ..................................................................................................................................2 1.4.3. Discussion ..............................................................................................................................2 1.5. Customizing Generated toString( ) Content ..........................................................................................4 1.5.1. Problem ..................................................................................................................................4 1.5.2. Solution ..................................................................................................................................4 1.5.3. Discussion ..............................................................................................................................5 1.6. Automating hashCode( ) and equals( ) .................................................................................................6 1.6.1. Problem ..................................................................................................................................6 1.6.2. Solution ..................................................................................................................................6 1.6.3. Discussion ..............................................................................................................................7 1.7. Automating compareTo( ) ...................................................................................................................8 1.7.1. Problem ..................................................................................................................................8 1.7.2. Solution ..................................................................................................................................8 1.7.3. Discussion ..............................................................................................................................8 1.7.4. See Also .................................................................................................................................9 1.8. Printing an Array ..............................................................................................................................10 1.8.1. Problem ................................................................................................................................10 1.8.2. Solution ................................................................................................................................10 1.8.3. Discussion ............................................................................................................................10 1.9. Cloning and Reversing Arrays ..........................................................................................................10 1.9.1. Problem ................................................................................................................................10 1.9.2. Solution ................................................................................................................................11 1.9.3. Discussion ............................................................................................................................11 1.10. Transforming Between Object Arrays and Primitive Arrays ..............................................................12 1.10.1. Problem ..............................................................................................................................12 1.10.2. Solution ..............................................................................................................................12 1.10.3. Discussion ..........................................................................................................................13 1.10.4. See Also .............................................................................................................................14 1.11. Finding Items in an Array ...............................................................................................................14 ii Common Java Cookbook 1.11.1. Problem ..............................................................................................................................14 1.11.2. Solution ..............................................................................................................................14 1.11.3. Discussion ..........................................................................................................................15 1.11.4. See Also .............................................................................................................................16 1.12. Creating a Map from a Multidimensional Array ...............................................................................16 1.12.1. Problem ..............................................................................................................................16 1.12.2. Solution ..............................................................................................................................16 1.12.3. Discussion ..........................................................................................................................16 1.12.4. See Also .............................................................................................................................16 1.13. Formatting Dates ............................................................................................................................17 1.13.1. Problem ..............................................................................................................................17 1.13.2. Solution ..............................................................................................................................17 1.13.3. Discussion ..........................................................................................................................17 1.13.4. See Also .............................................................................................................................18 1.14. Rounding Date Objects ...................................................................................................................18 1.14.1. Problem ..............................................................................................................................18 1.14.2. Solution ..............................................................................................................................19