(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>();

        // add Objects to map ...

        List keysList = new ArrayList(map.keySet());
        Collections.sort(keysList);

        System.out.print("[");
        for (Iterator<String> it = keysList.iterator(); it.hasNext(); )
{
            String key = it.next();
            Object value = map.get(key);
            System.out.print(key + "=" + value);
            if(it.hasNext())
                System.out.print(", ");
        }
        System.out.println("]");

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to