Monthly Archives: December 2014

Using JXTreeTable to display grouped data

Let me start by ranting about Swing. Its a lame duck. It could never really take off. But we can’t live with it, can’t live without it. Especially if the application is a legacy Swing application written 10 years ago.

So, when I had the prospect of displaying simple grouped data, the fear of writing my custom JTree absolutely seized me. That is when i thought about the SwingX project. In its present form, it buggy, unusable, messed up etc. It took me close to an hour to figure out a stable release from the quagmire of broken links that Google came up with. At last, the Maven Central came to my rescue in the form of the following 3 magical lines:

The First 90%

After being associated with Swing for a good part of my life, I have come to believe that 90% of your job is done if you can create a good Model, especially for unwieldy components like JTable and JTree. So, I do the same for this as well. In this case, I represent grouped data with a Map<String, List<BankDetail>>. All I want is to display the groups as nodes and the Lists under these nodes. This is how I model it:

The remaining 10%

The tying up the model to the JXTreeTable is the simplest thing on the earth:

This is how it looks like:

UsingJXTreeTable_1UsingJXTreeTable_2

 

The source code can be found here:

https://github.com/paawak/blog/tree/master/code/lambda-demo

The main class to run is: GroupingDemoFrame.

I found the following post very useful in my efforts:

http://javanbswing.blogspot.com/2013/08/swing-treetable-example-using.html

Using JDK 8 features to group data in a list

Use Case

Sorting has always been supported by Java. Recently, I came across a requirement in our project where we needed to group data in a list by some conditions, which can change based on the user selection. Traditionally, this would have been done by the sql statement’s GROUP BY clause in the DataAccess layer. But in our case, that would make the sql query very complex. So we decided to take the plunge and do the group by in the Service Layer using Java.

The Dao looks like this:

And the Service looks like this:

How to do it pre JDK 8

Pre JDK 8, Java does not provide group by out of the box. After ruling out hand-coding that logic, we decided to go with Google Guava’s ListMultimap. Its pretty cool actually. It stores in different values having the same key. Then, it exposes a List view of data with similar keys.

Note that we have to do it in 2 steps:

  1. Collect the data

  2. Iterate over the collected data and create a List view for each unique key

This sometimes becomes cumbersome, but is possibly one of the most elegant solutions pre JDK 8.

What changed in JDK 8?

Jdk 8 supports group by out of the box. No for loops, no 3rd party libraries. This is the code, short and sweet:

Again, note the absolute lack of for loops, which make the code crisp and more readable. Let me sum up how this is achieved:

  1. Create the Function for generating the key by which the data would be grouped. This Function would take a BankDetail object and supply a String. Note that the key can be any Object as long as it has a proper hashcode() method.

  2. Create a Collector using the Collectors.groupingBy() to collect the data

  3. From the un-grouped data, create a Stream, and apply the collector created above.

That simple!

The code can be found here:

https://github.com/paawak/blog/tree/master/code/lambda-demo