Java Streams Code Snippets

What is a Java Stream?

Java Stream is a sequence of elements supporting the sequential and parallel aggregate operation. This example shows an aggregate operation using Stream and IntStream

int sum =
                 .filter(w -> w.getColor() == RED)
                 .mapToInt(w -> w.getWeight())

In this example (from Oracle Stream doc), widgets is a Collection<Widget>. A stream of Widget objects was created via, filter it to product a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget. Finally this stream is summed to produce a total weight.


This post lists Java Streams code snippets that I find very useful

Read more

Lombok Tips

What is Lombok?

In their own word,

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more

Why Use Lombok

  • Reduce boilerplate code
  • Increase readability
  • Reduce work for unit test code coverage

When to NOT use Lombok?

  • More complex entity classes (non-POJO classes)

Read more

How to Update Eclipse Software on Linux behind proxy

To update eclipse software on Linux behind proxy is a bit tricky but doable and here is how: 1) Find the proxy server’s host name and your account’s authentication info 2) Open the network connection by “Windows”->”Preferences”-> “General”->”Network Connection”, select “manual” and enter the proxy information. Below is a screenshot of my setup:

Update to JDK 8 on Mac OS

Java 8 (1.8.11 at the time of the writing) is the latest Java version. It was released in March this year and contain various major updates, including the long-waited lambda support. This blog described how to update to JDK 8 on Mac OS and also to check the lambda support with a simple app.

Read more

Use alternatives utility to update JDK

At the prompt, type sudo update-alternatives –config java The current available java installs would be displayed Selection    Command ———————————————– *  1           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java 2           /usr/lib/jvm/jre-1.4.2-gcj/bin/java To get out of the prompt without make changes, hit Ctrl+C To add a new jdk, I downloaded the jdk-6u-linux-i586.bin from the Oracle Java site. Unzip it chmod 755 jdk-6u27-linux-i586.bin ./jdk-6u27-linux-i586.bin … Read more

Create Custom Widgets using InkScape and NetBeans Visual Library

This blog entry shows one how to create a special widget using InkScape and NetBeans Visual Library. This is part of my effort to create a Java-based implementation of MIT’s Scratch, which is implemented in Squeak Smalltalk.

Step 1 Create JScratch Block Graphics:

First I created the image using Inkscape (steps to follow), import the image to IDE, then create two LayerWidgets, one transparent, and another opaque. Add the image to the background LayerWidget, adn the Swing component to the front LayerWidget. Finally, use the BorderFactory.createEmptyBorder function to limit the size of the swing component at the front layer.

To create the Scratch blocks, I used InkScape, the Open Source SVG editor. Here are the steps I took to create the graphic:

Read more