Thursday, July 3, 2014

How to set the row id [ TR ] and column id [ TD] in displaytag?

First, I do not believe it is possible to set the ids on the td's or tr's in displaytag without modifying the source. This has not left my list of things to do, but for now I have a work around for you.
Instead of defining your table:
 id='row' name="..." export="true" requestURI="">
   property="usefulData" title="Useful data" sortable="true" />
  ... more columns ...
do this:
 id='row' name="..." export="true" requestURI="">
   title="Useful data" sortable="true" >
     id='${row.usefulData}' class='css_class_selector'>${row.usefulData}
  
Note the span wrapping the printed data. Now you can select the data relating printing inside your table, which is probably what you want; to select your data, as opposed to specifically selecting the td's and tr's.

Tuesday, July 1, 2014

Java: how can I split an ArrayList in multiple small ArrayLists?

You can use subList(int fromIndex, int toIndex) to get a view of a portion of the original list.
From the API:
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
Example:
    List<Integer> numbers = new ArrayList<Integer>(
        Arrays.asList(5,3,1,2,9,5,0,7)
    );
    List<Integer> head = numbers.subList(0, 4);
    List<Integer> tail = numbers.subList(4, 8);
    System.out.println(head); // prints "[5, 3, 1, 2]"
    System.out.println(tail); // prints "[9, 5, 0, 7]"
    Collections.sort(head);
    System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]"
    tail.add(-1);
    System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"
If you need these chopped lists to be NOT a view, then simply create a new List from the subList. Here's an example of putting a few of these things together:
// chops a list into non-view sublists of length L
static <T> List<List<T>> chopped(List<T> list, final int L) {
    List<List<T>> parts = new ArrayList<List<T>>();
    final int N = list.size();
    for (int i = 0; i < N; i += L) {
        parts.add(new ArrayList<T>(
            list.subList(i, Math.min(N, i + L)))
        );
    }
    return parts;
}


List<Integer> numbers = Collections.unmodifiableList(
    Arrays.asList(5,3,1,2,9,5,0,7)
);
List<List<Integer>> parts = chopped(numbers, 3);
System.out.println(parts); // prints "[[5, 3, 1], [2, 9, 5], [0, 7]]"
parts.get(0).add(-1);
System.out.println(parts); // prints "[[5, 3, 1, -1], [2, 9, 5], [0, 7]]"
System.out.println(numbers); // prints "[5, 3, 1, 2, 9, 5, 0, 7]" (unmodified!)
http://stackoverflow.com/questions/2895342/java-how-can-i-split-an-arraylist-in-multiple-small-arraylists

Efficient way to divide a list into lists of n size

You'll want to do something that makes use of List.subList(int, int) views rather than copying each sublist. To do this really easily, use Guava's Lists.partition(List, int) method:
List<Foo> foos = ...
for (List<Foo> partition : Lists.partition(foos, n)) {
  // do something with partition
}
Note that this, like many things, isn't very efficient with a List that isn't RandomAccess (such as a LinkedList).

Monday, June 30, 2014

Round a double to 2 decimal places

I think this is easier:
double time = 200.3456;
DecimalFormat df = new DecimalFormat("#.##");      
time = Double.valueOf(df.format(time));

System.out.println(time); // 200.35
Note that this will actually do the rounding for you, not just formatting.

Friday, June 27, 2014

MongoDB GUI client (cross-platform or Linux)

Wait for Robomongo - crossplatform MongoDB GUI client. Right now they published only Windows version and Mac OS X version coming soon (as noted on the site). I'm sure Linux version will be also available soon.
Update: Mac OS X and Linux versions released.
enter image description here