Thursday, 14 May 2015

Simple benchmarking : Immutable Collections VS Persistent Collections

Often you need to add new elements to a collection.
Because you are a good and careful developer you want to keep things immutable as much as possible. So adding a new element to an immutable collections will mean that you have to create a new immutable collection that contains all the elements of the original collections plus the new element.

You can create immutable collections using the guava library and also using the recent pCollection library.

In the following example, we will build 2 immutable lists, one immutable from guava and one persistent from pCollection.

They both will contain 10.000 integers initially.

We will create 20.000 immutable lists one for each type and we will measure the time taken.

package com.marco.pcollections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.pcollections.PCollection;
import org.pcollections.TreePVector;

public class PcollectionVSImmutable {
 public static void main(String[] args) {
  List<Integer> bigList = new ArrayList<Integer>();
  for (int i = 0; i < 10000; i++) {
   bigList.add(new Integer(i));
  Map<Integer, ImmutableList<Object>> allImmutable = new HashMap<Integer, ImmutableList<Object>>();
  Map<Integer, PCollection<Integer>> allPersistent = new HashMap<Integer, PCollection<Integer>>();
  PCollection<Integer> persistent = TreePVector.from(bigList);
  long start = System.currentTimeMillis();
  for (int i = 10000; i < 30000; i++) {
   allPersistent.put(new Integer(i), Integer(i)));
  System.out.println("creating 20.000 pCollections takes : " + (System.currentTimeMillis() - start) + "ms");
  ImmutableList<Integer> immutable = ImmutableList.copyOf(bigList);
  start = System.currentTimeMillis();
  for (int i = 10000; i < 30000; i++) {
   allImmutable.put(new Integer(i), ImmutableList.builder().addAll(immutable).add(new Integer(i)).build());
  System.out.println("creating 20.000 Guava ImmutableList takes : " + (System.currentTimeMillis() - start) + "ms");
  System.out.println("All immutable size : " + allImmutable.size() + " allPersistent size : " + allPersistent.size());

Output :

creating 20.000 pCollections takes : 29ms
creating 20.000 Guava ImmutableList takes : 18347ms
All immutable size : 20000 allPersistent size : 20000

Monday, 11 May 2015

Immutable Objects with Lombok - workshop

An immutable object is an object whose state cannot be modified after it is created.

Why Immutable Objects are important?

Because you have no total control : objects can be passed anywhere and can arrive from anywhere  in the code. If they are mutable, then any object can modify their state. Different threads can access the same model object and modify/read its state creating many problems difficult to handle.

What is an Immutable object?

Final private fields,  No Setters & Unmodifiable collections

An example ?

What does Lombok?

Project Lombok aims to reduce boilerplate codes by replacing them with a simple set of annotations.

How to install Lombok?

see here :

Workshop :

Create a model object called Home with the following fields :   

String address;
The street name of your home

int age;
How old were you when you enter this house

int fromYear;
Which year you start living there
int toYear;
Which year you left

Create a toString() method to print the details.

Create a class called MyHomes that prints the list of Homes you lived in.   

This class should create as many homes you lived in and print the details for each one of them. 

Now make the class immutable

Now try to clean the Home class code with Lombok!!

Hints / Immutability support :-


 How can we change a value? 

Add an immutable boolean field to indicate if the address has been verified.

Default should be false.

Add a method to MyHomes to verify a List of Homes

 Now try to use the @Wither annotation from Lombok!!

End of the workshop

Stretch content

Saturday, 2 May 2015

In the JVM 1 + 1 = 4

Assuming a and b are equals to 1 :

int a = 1;
int b = 1;

How many JVM operations you see in the following code?

int c = a + b;

The answer is 4 :


The JVM will load a and b (index 0 and 1 of the local variable array), add them together (in the operand stack), and will assign the result to c (index 2 of the local variable array).