Miga -
Fantastic. Thanks. It's much simpler and more elegant. I suppose it's
a matter of getting more familiar with the libraries. I somehow missed
TreeMap's putAll() functionality.
Best,
Dennis
On Oct 17, 12:02 am, miga <[EMAIL PROTECTED]> wrote:
> On Oct 16, 11:06 pm, dzgaga <[EMAIL PROTECTED]> wrote:> (3.1) Build and run a
> simple application that uses HashMap
>
> > Part 4 of this segment suggests for your own excercise, please do the
> > following ...
> > and then asks to "Display them in both unsorted and sorted order"
>
> > The best solution to displaying them in sorted order I could come up
> > with is listed below, but I'd be amazed if there wasn't an easier way.
> > Anyone else?
>
> > HashMap<String, Object> map = new HashMap<String, Object>();
>
> Instead of defining a HashMap object, I've defined a Map object:
> Map<String, Object> myHashMap = new HashMap<String, Object>();
>
> > // add Objects to map ...
>
> After populating the HasMap...
> Then I've used another Map as a TreeMap and its putAll method to get
> all the elements of the HashMap directly in sorted order:
> // Display it sorted
> Map<String, Object> myTreeMap = new TreeMap<String, Object>();
> myTreeMap.putAll(myHashMap);
> System.out.println("My hash map in sorted order" + myTreeMap);
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---