How to join values and include prefix and suffix with StringJoiner in Java

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
In this example we use values and want to join them into one String value separated with comma. We also want to include a prefix and a suffix in that String in form of brackets:

The output from the above code will be:

Now, since the add method of StringJoiner returns itself, a more fluent way of writing would be:

How to create a comma separated string using StringJoiner in Java

This small example shows how to create a comma separated value using the StringJoiner class. The StringJoiner class takes a delimiter as parameter to its constructor which is appended after each value that is added (except for the last one). Finally we use the toString method to print out the value, like this:

The output from the above will look like this:

How to initialize a static map in Java

This example shows how to initialize a static map. It actually contains two examples, the first one can be used with any version of Java and the second one can be used from Java 8 and forward.

In the example below we initialize two static maps in two different ways. The first map is initialized with a static code block, or a static initializer. The second static map is initialized using an instance initializer. Finally the contents of both static maps are printed out in the main method.

The output from the code above is:

Using Java 8 features to initialize a static map

The second example also initialize a static map, but with lambda expressions:

The output from the code above:

Convert list from one type to another using lambda expressions in Java

This example shows how to convert a list of a certain type of objects to a list with another type of objects using lambda expressions (available from Java 8).

First we create to classes to work with, Employee and Person. The employee has got three attributes and the Person class has just one that matches one the attributes from Employee, the name.

Then we create an ArrayList of employee objects and add one object to it. Finally we do the conversion like this:

Draw a draggable triangle in Java

This example shows how to draw a triangle on a JFrame and make it draggable so it can be moved around by using the mouse. The example contains two classes, one which is the main frame (the JFrame) and one that extends JPanel that does the actual painting and that implements the necessary interfaces needed to make the triangle draggable.

Continue reading

Java interface default methods

Java interfaces cannot implement code in the methods declared, instead as you probably know they have to be overridden and implemented in the class that implement that particular interface. However, one feature in Java since version 8 is that you can define methods as default in an interface and provide the implementation for it as well, which means that the class or classes that implement that particular interface don’t have to override those methods, but they can still be reached from the caller of that class.

Continue reading