Many of the Java examples on our web site that are used to illustrate the utility classes in Java.util are written using String objects as both key and value - however, on the Java course just completed I wrote a somewhat more complex examples to show the relationship between various aspects of the language.
Using a buffered reader, I've read in a series of records from a file and passed each one on to a factory method which analyses that text string and returns an object. Using an accessor method, I extract a text key that I'm going to use to key that object, and I then add the object into a HashMap. See the source code at
[here].
The internals of how the input string is converted into an object, and how the properties are accesses, is encapsulated in the class, using a factory method and conventional accessors - see
[here].
Once the has map has been set up, my sample program picks out and reports on a chosen element by its key, and I've also demonstrated a set and an iterator to parse the whole Hashmap.
This week, I ran a private Java course for five delegates - 4 days of "Learning to program in Java" - and I thoroughly enjoyed myself. I'm delighted to run such basics courses in Java, although there are so many specialist advanced libraries in Java that we can rarely cover individual customer's detailed specialist requirements these days - hence we no longer offer a standard public course. But please ask if interested!
(written 2014-11-27, updated 2014-11-28)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[2322] Looking for a practical standards course - (2009-08-05)
[2741] What is a factory? - (2010-04-26)
[2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
[3608] Design Patterns - what are they? Why use them? - (2012-02-12)
[3716] Learning C++ - a design pattern for your first class - (2012-05-02)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3843] Caching Design Patterns - (2012-08-20)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4359] How to avoid too many recalculations within an object - (2014-12-21)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
[4421] How healthy are the stars of stage and screen? - (2015-02-09)
[4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
[4626] Singleton design pattern - examples and uses - (2016-01-20)
[4663] Easy data to object mapping (csv and Python) - (2016-03-24)
[4673] Separating detailed data code from the main application - Ruby example - (2016-05-16)
J714 - Java - Fundamental classes [42] Do languages change? - (2004-09-08)
[1062] Java sorting - comparable v comparator - (2007-02-02)
[1502] Java, sorting, ArrayList example, generics - (2008-01-11)
[1910] Java - Generics - (2008-11-27)
[2323] Java Collection Objects in the java.util package - (2009-08-05)
[2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
[2421] Sorting Collections of Objects in Java - (2009-09-25)
[2649] Length, size or capacity in Java? - (2010-02-24)
[2734] for and foreach in Java - (2010-04-22)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3048] String handling - from first steps to practical examples - (2010-11-13)
[4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
Some other Articles
Splitting out code into name blocks for clarity and reusabilityMisty Melksham MorningFirst Java Application - calculating the weight of a tableclothMusings on a Welsh townJava - factory method, encapsulation, hashmap exampleDoes Santa Claus need a CRB check?Folk music train, Westbury to Swindon round trip, 14th December 2014Good, stable, reliable local businessesLearning to program - comments, documentation and test code