The Source for Java Technology Collaboration
User: Password:



Hans Muller

Hans Muller's Blog

Open Source Bluegrass

Posted by hansmuller on October 05, 2005 at 01:23 PM | Comments (6)

This past Saturday morning, a friend and I were in San Francisco at Golden Gate park, walking out of the Speedway Meadow. It was cool and overcast and the fog blanketed the tree tops and hung over our heads and gave the surroundings the blurry hazy look of an old newsreel. We were walking down into another hollow, listed on the map as Marx meadow, and drifting toward us was the sound of Joan Baez singing "The Night They Drove Old Dixie Down". The long narrow meadow is flanked by steep hills and tall trees. With its foggy ceiling and dim light it felt like a cathedral. There were thousands of people there, listening, shuffling toward the stage for a better view or just sitting in the grass listening to the story about Virgil Caine and the tragedy of the American Civil War. We joined them and listened to Joan Baez sing more old American songs, songs by Woody Guthrie and Bob Dylan and folk songs that have been around so long they're just attributed to "traditional". I'll never forget being the same place last year listening to John Prine sing "Angel from Montgomery" or this year, hearing anti-war and protest songs inspired by conflicts from the distant past and from right now.

It's tough to write about how great it was spending a day listening to music. It was entertaining, it was moving, it was fun, sometimes it was beautiful. In the evening it was really cold. You should have been there. If you live in the area, you should have stepped away from the keyboard and taken your head out of the network for a while, and plugged it into the music. At least that's what I did and I couldn't have been happier. But that's not why I'm writing this.

There were five big stages in Golden Park on Saturday and Sunday, different bluegrass and folk music performers every hour, from 11AM till 7PM, and it was all free. Free as in Free Beer. A (very) wealthy man named Warren Hellman has been putting on the "Not Strictly Bluegrass Festival" show on his own dime for the past five years. I can't tell you exactly why he does this, although if I had to guess I'd say he's motivated by the same spirit that inspires software engineers to join open source projects and build things just for the pure joy of it. That spirit confounds economists looking for a profit motive and capitalists looking for a profit. There were about a bajillion people in Golden Gate Park taking in the music over the weekend, and they weren't confused at all.

So I just wanted to say thank you. Thank you Warren Hellman for sharing two days of music with the Bay Area, again.

If you've actually read this far and have made a mental note to delete me and my moon-eyed ravings from your RSS feed, here's a non-sequitur that I hope you'll find reassuring. It doesn't have anything to do with free music, but it's a genuine Java code sample that you might find useful.

Examples from the java.util canon are usually short and tidy. In theory, now that we have type parameters, the can be even shorter and tidier. For example if you have a TreeMap that defines the number of occurrences of each word in a document, you can print them all like this:

Map<String, Integer> countMap = new TreeMap<String, Integer>();
...
for (Map.Entry<String, Integer> e: countMap.entries()) {
    System.out.println(e.getKey() + ":" + e.getValue());
}

All well and good, unless you wanted to see the word/count entries sorted by word count, not by word. TreeMap keeps the entries sorted by key, that's the word String in this case. To sort the word/count Map.Entry entries by word count you must convert the entries to a List, define a Comparator that compares Map.Entry values, and then use Collections.sort to sort the list.

/* Sort the entries in countMap by count.  This code has
 * me wondering about just going back to AWK.
 */
Comparator<Map.Entry<String, Integer>> compareEntries =
    new Comparator<Map.Entry<String, Integer>>() {
    public int compare(Map.Entry<String, Integer>e1, Map.Entry<String, Integer>e2) {
        return e2.getValue().compareTo(e1.getValue());
    }
};
ArrayList<Map.Entry<String, Integer>> entries =
    new ArrayList<Map.Entry<String, Integer>>(countMap.entrySet());
Collections.sort(entries, compareEntries);

Maybe it's just me, but yech. Sometimes I'd really rather just listen to Earl Scruggs play Foggy Mountain Breakdown.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Curious code.

    In general that implementation of compare doesn't work, even for integers. It happens that you are unlikely to get values that are not non-negative ints.

    Given that Long is Comparable, you might find it easier to write a general Comparable implementation that compares the value of Map.Entry objects with Comparable values..

    Posted by: tackline on October 05, 2005 at 01:52 PM

  • Hmmmm, watching Earl Scruggs is fun but I've only had the pleasure via DVD. The Three Pickers (Scruggs, Doc Watson and Ricky Scaggs) is a truely jaw dropping experience.

    Posted by: jaseb on October 05, 2005 at 03:28 PM

  • Tackline commented that:

    > In general that implementation of compare doesn't work,
    > even for integers. It happens that you are unlikely to
    > get values that are not non-negative ints.

    Very unlikely indeed, since the app is computing the number
    of occurrences of words in a document. However the example
    is confusing since I (inexplicably) used Longs for the sums,
    when Integers would been fine and would have obviated the
    cast to int. I've changed the example. It's still ugly.

    Posted by: hansmuller on October 06, 2005 at 09:30 AM

  • Jaseb mentioned the Three Pickers CD. I haven't seen it but have
    heard good things about it; will have to find a copy! Both
    Earl Scruggs and Doc Watson performed on Saturday. Both
    of them are now octogenarians and they were joined by their sons.
    Gary Scruggs played electric bass and did a spot-on impression
    of Dylan when they played "You Ain't Goin' Nowhere".

    Posted by: hansmuller on October 06, 2005 at 09:37 AM

  • Why not use

    e1.getValue().compareTo(e2.getValue())

    Posted by: mthornton on October 07, 2005 at 06:42 AM

  • You are right (mthornton), using Integer.compareTo() is
    cleaner than just substracting the two values. I've updated the
    example; thanks for the suggestion!

    Posted by: hansmuller on October 07, 2005 at 09:13 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds