Minborg

Minborg
Minborg

Wednesday, May 16, 2018

Java Stream ORM Now with JOINs

Java Stream ORM Now with JOINs

Speedment is a Java Stream ORM Toolkit and Runtime that allows you to view database tables as standard Java Streams. Because you do not have to mix Java and SQL, the application becomes much more compact making it faster to develop, less prone to errors and easier to maintain. Streams are also strictly type-safe and lazily constructed so that only a minimum amount of data is pulled in from the database as elements are consumed by the streams.

The new version Speedment 3.1.1 “Homer” now also supports dynamically joined tables to be viewed as standard Java Streams. This is a big deal when developing Java applications that explore relations between database tables.

In the examples below, I have used the open-source Sakila film database content for MySQL that you can download here. Speedment works for any major relational database type such as Oracle, MySQL, Microsoft SQL Server, PostgreSQL, DB2, MariaDB, AS400 and more.

Streaming over a Single Table

The following code snippet will create a List of all Film objects that has a Film.RATING of “PG-13” and where the List is sorted in Film.LENGTH order:
List<Film> list = films.stream()
    .filter(Film.RATING.equal("PG-13"))
    .sorted(Film.LENGTH)
    .collect(toList());

The stream will be automatically rendered to a SQL query under the hood. If we enable Stream logging, we will see the following (prepared statement “?”-variables given as values in the end):
SELECT 
    `film_id`,`title`,`description`,`release_year`,
    `language_id`,`original_language_id`,
    `rental_duration`,`rental_rate`,
    `length`,`replacement_cost`,`rating`,`special_features`,
    `last_update` 
FROM 
    `sakila`.`film` 
WHERE 
    (`rating`  = ? COLLATE utf8_bin) 
ORDER BY
    `length` ASC

values:[PG-13]

Thus, the advantage is that you can express your database queries using type-safe Java and then consume the result by means of standard Java Streams. You do not have to write any SQL code.

Joining Several Tables

Appart from the table “film”, the Sakila database also contains other tables. One of these is a table called “language”. Each Film entity has a foreign key to the Language being spoken in the film using a column named “language_id”.

In this example, I will show how we can create a standard Java Stream that represents a join of these two tables. This way, we can get a Java Stream of matching pairs of Film/Language entities.

Join objects are created using the JoinComponent which can be obtained like this:
// Visit https://github.com/speedment/speedment
// to see how a Speedment app is created. It is easy!
Speedment app = …;

JoinComponent joinComponent = app.getOrThrow(JoinComponent.class);

Once we have grabbed the JoinComponent, we can start creating Join objects like this:
Join<Tuple2<Film, Language>> join = joinComponent
    .from(FilmManager.IDENTIFIER)
    .innerJoinOn(Language.LANGUAGE_ID).equal(Film.LANGUAGE_ID)
    .build(Tuples::of);

Now that we have defined our Join object we can create the actual Java Stream:
join.stream()
    .map(t2 -> String.format(
        "The film '%s' is in %s",
            t2.get0().getTitle(), // get0() -> Film
            t2.get1().getName()   // get1() -> Language
    ))
    .forEach(System.out::println);

This will produce the following output:
The film 'ACADEMY DINOSAUR' is in English
The film 'ACE GOLDFINGER' is in English
The film 'ADAPTATION HOLES' is in English
...

In the code above, the method t2.get0() will retrieve the first element from the tuple (a Film) whereas the method t2.get1() will retrieve the second element from the tuple (a Language). Default generic tuples are built into Speedment and thus Tuple2 is not a Guava class. Speedment does not depend on any other library. Below you will see how you can use any class constructor for the joined tables. Again, Speedment will render SQL code automatically from Java and convert the result to a Java Stream. If we enable Stream logging, we can see exactly how the SQL code was rendered:
SELECT
    A.`film_id`,A.`title`,A.`description`,
    A.`release_year`,A.`language_id`,A.`original_language_id`,
    A.`rental_duration`,A.`rental_rate`,A.`length`,
    A.`replacement_cost`,A.`rating`,A.`special_features`,
    A.`last_update`,
    B.`language_id`,B.`name`,B.`last_update` 
FROM 
    `sakila`.`film` AS A
INNER JOIN 
    `sakila`.`language` AS B 
ON
    (B.`language_id` = A.`language_id`)

Interestingly, the Join object can be created once and be re-used over and over again to create new Streams.

Many-to-Many Relations

The Sakila database also defines a handful of Many-to-Many relations. For example, the table “film_actor” contains rows links films to actors. Each film can have multiple actors and each actor might have appeared in multiple films. Every row in the table links a particular Film to a specific Actor. For example, If a Film depicts 12 Actor entities, then FilmActor contains 12 entries all having the same film_id but different actor_ids. The purpose of this example is to create a complete list of all films and the appearing actors in a Java Stream. This is how we can join the three tables together:
Join<Tuple3<FilmActor, Film, Actor>> join = joinComponent
    .from(FilmActorManager.IDENTIFIER)
    .innerJoinOn(Film.FILM_ID).equal(FilmActor.FILM_ID)
    .innerJoinOn(Actor.ACTOR_ID).equal(FilmActor.ACTOR_ID)
    .build(Tuples::of);

    join.stream()
        .forEach(System.out::println);


The code above will produce the following output (formatted for readability):
...
Tuple3Impl {
    FilmActorImpl { actorId = 137, filmId = 249, lastUpdate = 2006-02-15 05:05:03.0 },
    FilmImpl { filmId = 249, title = DRACULA CRYSTAL, description =...,
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS,...}
}

Tuple3Impl {
    FilmActorImpl { actorId = 137, filmId = 254, lastUpdate = 2006-02-15 05:05:03.0 },
    FilmImpl { filmId = 254, title = DRIVER ANNIE, description = ...,
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS, ...}
}

Tuple3Impl {
    FilmActorImpl { actorId = 137, filmId = 263, lastUpdate = 2006-02-15 05:05:03.0 },
    FilmImpl { filmId = 263, title = DURHAM PANKY, description = ... },
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS,... }
}
...

Joins with Custom Tuples

As we noticed in the example above, we have no actual use of the FilmActor object in the Stream since it is only used to link Film and Actor objects together during the Join phase.

When Join objects are built using the build() method, we can provide a custom constructor that we want to apply on the incoming entities from the database. The constructor can be of any type so you can write your own Java objects that holds, for example, Film and Actor or any of the columns they contain and that are of interest.

In this example, I proved a (lambda) constructor that just discards the linking FilmActor objects altogether:
Join<Tuple2<Film, Actor>> join = joinComponent
    .from(FilmActorManager.IDENTIFIER)
    .innerJoinOn(Film.FILM_ID).equal(FilmActor.FILM_ID)
    .innerJoinOn(Actor.ACTOR_ID).equal(FilmActor.ACTOR_ID)
    .build((fa, f, a) -> Tuples.of(f, a));

    join.stream()
        .forEach(System.out::println);

The code above will produce the following output (formatted for readability):
...
Tuple2Impl {
    FilmImpl { filmId = 249, title = DRACULA CRYSTAL, description = ... },
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS, ...}
}
Tuple2Impl {
    FilmImpl { filmId = 254, title = DRIVER ANNIE, description = A... }, 
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS,...}
}
Tuple2Impl {
    FilmImpl { filmId = 263, title = DURHAM PANKY, description = ... }, 
    ActorImpl { actorId = 137, firstName = MORGAN, lastName = WILLIAMS,...}
}
...
Thus, we only get matching pairs of Film and Actor entities where there is an appearance of an actor in a film. The linking object FilmActor is never seen in the Stream.

Take it for a Spin!

Over the course of this article, you have learned how to stream over one or several database tables using Speedment.

Visit Speedment open-source on GitHub and try it out!

Read all about the new JOIN functionality in the User's Guide.

Wednesday, November 22, 2017

Easily Return Values From a Transaction with Speedment 3.0.17 or Later

Transactions

In my previous post, I wrote about how to use Transactions in an easy way using Speedment where we updated two bank accounts atomically. As you all might know, transactions are a way of combining a number of database operation into a single operation that is atomically executed.

But transactions are not only about updating the database but also about performing atomic reads. With Speedment, we can compute values atomically using Java streams and then return the result to something outside the scope of the transaction in an easy way.

Returning Transaction Values

Suppose we have a database with films and artists (e.g. using the open-sourced Sakila database content for MySQL) and we would like to count the number of films plus the number of artist in the database in a single transaction. This might be the case if we want to show some kind of database size in a GUI or the likes. Here is how it might look like:

    long sumCount = txHandler.createAndApply(tx -> 
        films.stream().count() + actors.stream().count()
    );

When the transaction starts, the view of the database is "frozen" so that new films or artist that are inserted into the database by other threads are not visible within the transaction. Thus, we can safely assume that our view is unaffected by other threads.

As can be seen, with really minimal boiler plate code, we can express functions that are to be performed within transactions using Speedment.

Preparations

Before transactions can be used, we need to obtain a TransactionHandler like this:

    SakilaApplication app = ....
    TransactionComponent transactionComponent = app.getOrThrow(TransactionComponent.class);
    TransactionHandler txHandler = transactionComponent.createTransactionHandler();

The FilmManager and the ActorManager can be retrieved from the application as shown hereunder:

    FilmManager films = app.getOrThrow(FilmManager.class);
    ActorManager actors = app.getOrThrow(ActorManager.class);

Read more on Speedment transactions here.

What's Next?


Visit Speedment open-source on GitHub and try it out!

Tuesday, November 14, 2017

Transactions Made Simple Using Speedment 3.0.17 and Above

Transactions

Sometimes we want to make sure that our database operations are executed atomically and separated from other operations. This is where transactions come into play. A transaction is a set of operation
proposals that may or may not be accepted by the database as an atomic operation. So, either all operations in the transaction are accepted or no operations of the transaction are accepted. Another advantage with transaction is that the state of the database will be locally "frozen" when the transaction starts, so we will not see updates by other threads while in the transaction.

Speedment is an open-source Stream ORM Java Toolkit and Runtime Java tool that wraps an existing database and its tables into Java 8 streams. Later versions of Speedment support database transactions in an easy-to-use way.

Updates

Imagine we are writing a bank application with accounts and we are going to move $100 from one account (1) to another (2). In this case it is important that money does not disappear (i.e. is deducted from 1 but never deposited in 2) or perhaps even worse, will be duplicated (i.e. deposited in 2 but not deducted from 1). This can be assured using a Speedment database transaction like this:
txHandler.createAndAccept(tx ->

    Account sender = accounts.stream()
        .filter(Account.ID.equal(1))
        .findAny()
        .get();

    Account receiver = accounts.stream()
        .filter(Account.ID.equal(2))
        .findAny()
        .get();

    accounts.update(sender.setBalance(sender.getBalance() - 100));
    accounts.update(receiver.setBalance(receiver.getBalance() + 100));

    tx.commit();
}
When the method tx.commit() is called, the two updates are committed to the database atomically and will be visible to all other threads. If we do not call tx.commit() explicitly then the transaction will be automatically rolled back (i.e. the updates will not have any effect and will be discarded).

Preparations

Before transactions can be used, we need to obtain a TransactionHandler like this:
    BankApplication app = ....
    TransactionComponent transactionComponent = app.getOrThrow(TransactionComponent.class);
    TransactionHandler txHandler = transactionComponent.createTransactionHandler();
The AccountManager can be retrieved from the application as shown hereunder:
    AccountManager accounts = app.getOrThrow(AccountManager.class);

What's Next?

Read more on Speedment transactions here.

Visit GitHub and read all about Speedment open-source here.

Monday, September 11, 2017

The Need for Speed, Access Existing Data 1,000x Faster

Learn how you can speed up your analytics database applications by a factor of 1,000 by using standard Java 8 streams and Speedment’s In-JVM-Memory accelerator.

Web and mobile applications are sometimes slow because the backing database is slow and/or the connection to the database imposes latencies. Modern UIs and interactive applications require fast back-ends with ideally no observable latency or else users will move on to other services or will just get tired and stop using the service altogether.

In this article we will learn how analytic database applications can be speed up by orders of magnitude using standard Java 8 streams and Speedment's in-JVM-memory acceleration technology. At the end, we will run a JMH test suit with representative benchmarks that indicate a speedup factor exceeding 1,000 times.

Viewing the Database as Streams

Speedment is a modern stream based ORM meaning that the tables are viewed as standard Java 8 streams. In this article we will use the “Sakila” database that is an open-source example database available directly from Oracle here. The Sakila example database contains films, actors, etcetera. This is how a Java 8 stream from the database might look like:

    List<Film> secondPage = films.stream()
        .filter(Film.RATING.equal("PG-13"))
        .sorted(Film.TITLE.comparator())
        .skip(50)
        .limit(50)
        .collect(Collectors.toList());

This stream will filter out only those films that has a rating equal to “PG-13” and will then sort the remaining films by film title. After that, the first 50 films are skipped and then the next 50 films are collected to a list. Thus, we get the second page of all PG-13 films sorted in title order.

Usually, we also need to know how many films there are in total that has a rating of “PG-13” in order to show a correctly scaled scrollbar in our application. This can be done like this:

     long count = films.stream()
        .filter(Film.RATING.equal("PG-13"))
        .count();

Using a Database

Speedment will automatically render the Streams to SQL. This way, we can remain in a pure typesafe Java environment without having to write SQL code. By enabling logging, we can see that the first paging stream will be rendered to the following SQL query (assuming we are using MySQL):
 
SELECT
    `film_id`,`title`,`description``release_year`,
    `language_id`,`original_language_id`,`rental_duration`,`rental_rate`,
    `length`,`replacement_cost`,`rating`,`special_features`,`last_update` 
FROM 
    `sakila`.`film` 
WHERE 
    (`sakila`.`film`.`rating`  = ? COLLATE utf8_bin) 
ORDER BY 
    `sakila`.`film`.`title` ASC 
LIMIT ? OFFSET ?

values:[PG-13, 50, 50]
The second count stream will be rendered to:
 
SELECT COUNT(*) FROM (
    SELECT
        `film_id`,`title`,`description``release_year`,
        `language_id`,`original_language_id`,`rental_duration`,`rental_rate`,
        `length`,`replacement_cost`,`rating`,`special_features`,`last_update`
     FROM
             `sakila`.`film` 
        WHERE 
            (`sakila`.`film`.`rating`  = ? COLLATE utf8_bin)
) AS A

values:[PG-13]

Thus, the stream operations are rendered to efficient SQL. When running one thousand of these queries in parallel on a laptop class of computer with the MySQL standard server configuration, they complete in 700 ms and 175 ms aggregated latency respectively. If you are thinking about how the second SQL statement could be efficient, the fact is that the database will be able to basically eliminate the inner select.

Using In-JVM-Memory Acceleration

Now to the fun part. Let us activate the in-JVM-memory acceleration component in Speedment, called DataStore, in our application. This is done in this way:

     SakilaApplication app = new SakilaApplicationBuilder()
        .withPassword("sakila-password")
        // Activate DataStore
        .withBundle(DataStoreBundle.class)
        .build();

        // Load a snapshot of the database into off heap memory
        app.get(DataStoreComponent.class)
            .ifPresent(DataStoreComponent::load);

When the application is started, a snapshot of the database is pulled into the JVM and is stored off-heap. Because data is stored off-heap, data will not influence garbage collection and the amount of data is only limited by the available RAM. Nothing prevents us from loading terabytes of data if we have that amount of RAM available.

If we now run the same application again, we get 22 ms and 1 ms aggregated latency. This means that the latency is reduced by a factor of 30 and 170 respectively. A significant improvement it has to be said. But, it is getting better still.

Using In-JVM-Memory Acceleration and Json

REST and JSON are commonly used to serve clients that request data these days. Speedment has a special collector that can collect JSON data using something called in-place de-serialization whereby only the fields that are needed by the collector are deserialized from off-heap memory. We can depend on the Json plugin by first adding a dependency in our pom file:

     <dependency>
        <groupId>com.speedment.enterprise.plugins</groupId>
        <artifactId>json-stream</artifactId>
        <version>${speedment.enterprise.version}</version>
    </dependency>

Then, we install the plugin in the ApplicationBuilder as shown hereunder:


     SakilaApplication app = new SakilaApplicationBuilder()
        .withPassword("sakila-password")
        .withBundle(DataStoreBundle.class)
        // Install the Json Plugin
        .withBundle(JsonBundle.class)
        .build();

If we only want the Film fields “title”, “rating” and “length” in the json output, we can create a Json encoder like this:


    final JsonComponent json = app.getOrThrow(JsonComponent.class);

    final JsonEncoder<Film> filmEncoder = json.<Film>emptyEncoder()
        .put(Film.TITLE)
        .put(Film.RATING)
        .put(Film.LENGTH)
        .build();

This decoder is immutable and can be reused over and over again in our application:


     String json = films.stream()
        .filter(Film.RATING.equal("PG-13"))
        .sorted(Film.TITLE.comparator())
        .skip(50 * pageNo)
        .limit(50)
        .collect(JsonCollectors.toList(filmEncoder));

This gives us an additional speed up factor of 2 compared to dealing with entire entities. The JsonComponent can do much more than simply collecting things to a list. For example, it can  create aggregations, also using in-place de-serialization. 



Run Your Own Projects with In-JVM-Memory Acceleration

It is easy to try in-JVM-Memory acceleration by yourself. There is a free Initializer that can be found here. Just tick in your desired database type and you will get a POM and an application template automatically generated for you. You also need a license key to run. Just click “Request a Free Trial License Key” on the same page to get one. If you need more help setting up your project, check out the Speedment GitHub page or explore the manual.

How Fast is it for Real?

Speedment supports a number of database types including Oracle, MySQL, MariaDB, PostgreSQL, Microsoft SQL Server, DB2 and AS400. Speedment can also work with Avro files that is used by Hadoop. In this example, we will run MySQL.

Testing performance in Java application is notoriously difficult. Using the JMH framework, I have written a number of typical applications and have run each test hundreds of thousand times and compared the result for pure MySQL and MySQL with Speedment’s in-JVM accelerator. The performance figures below are given as operations/second (higher is better).


Benchmark
Pure MySQL
MySQL with Speedment in-JVM
Speed up factor
Count all
5,324
43,615,967
8,000
Count with filter
5,107
2,465,928
400
Filtering
449
597,702
1,300
Sorting
109
171,304
1,500
Paging
1,547
1,443,015
900
Iterate all
108
5,556
50
Aggregation
117
167,728
1,400
Aggregation filter
453
608,763
1,300

As can be seen, MySQL with Speedment In-JVM accelerator outperforms Pure MySQL by a factor of 1,000 or more in most cases. The smallest speed up factor observed was 50 times which is still very good.

Test Environment

MySQL, 5.7.16 standard installation, MySQL JDBC Driver 5.1.42, Oracle Java 1.8.0_131, Speedment Enterprise 1.1.10, macOS Sierra 10.12.6, Macbook Pro 2.2 GHz i7 (mid 2015), 16 GB RAM.

Benchmark Code

Below are some examples of how the benchmark code looks like. The complete benchmark application can be found on GitHub here. I encourage you to clone it an run it to see the speedup factor on your own target machine.
    @Benchmark
    public String paging() {
        return films.stream()
            .filter(Film.RATING.equal("PG-13"))
            .skip(50)
            .limit(50)
            .collect(filmCollector);
    }


    @Benchmark
    public String aggregationWithFilter() {
        return films.stream()
            .filter(Film.RATING.equal("PG-13"))
            .collect(sumLengthCollector);
    }

How Much RAM is Needed?

Speedment can often store data in RAM more efficiently than the database itself. The Sakila database in the benchmark takes 6.6 MB on disk but Speedment is only using 3 MB of memory. Considering that Speedment indexes all columns by default whereas the database only indexes a few columns, the Speedment is remarkably memory efficient.

How Long Time Does it Take to Load Data?

The Sakila database was loaded and indexed by Speedment in less than 1 second. Speedment can refresh data from the database in the background and will keep track of which streams are running against which database snapshot version (MVCC).

How Much Faster Will My Own Applications Run?

How much latency reduction one would get in a particular project is anyone's guess. Is is x10, x50, x100 or even more? Take the opportunity and find out how much speed you can gain in your own projects!

Take it for a Spin

Learn more about Speedment on GitHub and kick-start your own project using the Speedment Initializer and remember to tick “enable in-memory acceleration” and use the initializer to get a free evaluation license key too. Explore the manual section for the Speedment in-JVM accelerator here or use my twitter handle @PMinborg

Thursday, June 8, 2017

Use Precise Java Method Parameters

Learn how to pick the right method parameter types and get more robust and shorter code in your Java applications.

We Java developers generally have a bad habit of using method parameters without thinking of what is actually needed and just picking whatever we are used to, what we have available or whatever first comes into mind. Consider the following representative example:
 
    private static String poem(Map<Integer, String> numberToWord) {
        return new StringBuilder()
            .append("There can be only ")
            .append(numberToWord.get(1))
            .append(" of you.\n")
            .append("Harts are better off when there are ")
            .append(numberToWord.get(2))
            .append(" of them together.\n")
            .append("These ")
            .append(numberToWord.get(3))
            .append(" red roses are a symbol of my love to you.\n")
            .toString();
    }

When we use the method above, we provide a Map that translates from a number to a String. We might, for example, provide the following map:

    Map<Integer, String> englishMap = new HashMap<>();
        englishMap.put(1, "one");
        englishMap.put(2, "two");
        englishMap.put(3, "three");

When we call our poem method with the englishMap then the method will produce the following output:
 
There can be only one of you.
Harts are better off when there are two of them together.
These three red roses are a symbol of my love to you.

That sounds good. Now suppose that your significant other is a computer nerd and you want to spice up your poem and make an impression, then this is the way to go:

    Map<Integer, String> nerdMap = new HashMap<>();
        nerdMap.put(1, "1");
        nerdMap.put(2, "10");
        nerdMap.put(3, "11");

If we now submit our nerdMap to the poem method, it will produce the following poem:
 
There can be only 1 of you.
Harts are better off when there are 10 of them together.
These 11 red roses are a symbol of my love to you.

As with all poems, it is difficult to judge which poem is more romantic that the other but I certainly have my own personal view.
 


The Problems

There are several problems with the solution above:

First of all, as an outside caller, we cannot be sure that the poem method does not change the Map we provide. After all, we provide a Map and there is nothing preventing a receiver to do whatever possible with the map, even clearing the entire map altogether. This can of course be avoided by wrapping the Map using the Collections.unmodifiableMap() method or provide a copy of an existing map whereby the copy is later discarded.

Secondly, we are tied to use a Map when we only need something that translates from an integer to String. This might create unnecessary code in some cases. Think back of our nerdMap, where the values in the map could easily be computed using the Integer::toBinaryString instead of mapping them manually.

The Solution

We should strive to provide precisely what is needed in any given situation and not more. In our example we should modify the poem method to take a function that goes from an integer to a String. How this function is implemented on the caller side is of less importance, it can be a map or a function, or code or something else. Here is how it should be done in the first place:
 
    private static String poem(IntFunction<String> numberToWord) {
        return new StringBuilder()
            .append("There can be only ")
            .append(numberToWord.apply(1))
            .append(" of you.\n")
            .append("Harts are better off when there are ")
            .append(numberToWord.apply(2))
            .append(" of them together.\n")
            .append("These ")
            .append(numberToWord.apply(3))
            .append(" red roses are a symbol of my love to you.\n")
            .toString();
    }
 
If we want to use the poem method with a Map, we simply call it like this:
 
    // Expose only the Map::get method
    System.out.println(poem(englishMap::get));

If we want to compute the values like we did for the the nerd poem then we can do it even simpler:


    System.out.println(poem(Integer::toBinaryString));

Heck, we can even produce a poem to a significant other suffering from a dual personality disorder like this:
 
    System.out.println(
        poem(
            no -> englishMap.getOrDefault(no + 1, Integer.toString(no + 1))
        )
    );

This will produce the following poem:

  
There can be only two of you.
Harts are better off when there are three of them together.
These 4 red roses are a symbol of my love to you.

Be careful with your method parameters!

Sunday, December 25, 2016

Day 25, Java Holiday Calendar 2016, The Complete Deck

Day 25, Java Holiday Calendar 2016, The Complete Deck



Thank you all for following this year's Java Calendar. Here is the complete list with all the articles:



1: Use the @FunctionalInterface Annotation
If you want to ensure API users can use lambdas to implement your interface, it's important to make sure your code is annotated properly. Check out how to do it.

2: Favor Composition Over Inheritance
When you're working with your classes, make sure to use composition when you can for better separation of concerns, among a variety of other reasons. See how it's done.

3: Initializing Maps in the Smartest Way
Learn how you can create and initialize type-safe maps in Java by using two simple utility methods.

4: Use RemoveIf in Java Collections
If you want to remove elements from a collection quick, fast, and in a hurry, take a look at removeIf(), which should save you ton of time over manual iterations.

5: CRUD Operations
See how, with a handy open-source tool, you can alter your database entities, with standard CRUD operations, in Java.

6: Be Lazy With Java 8
If you want your code to kick in only when you need it, then lazy initialization is just right for you. See how you can get it working in Java 8.

7: Access Databases With Streams
Want to query your database with Java? See how Speedment and Java 8's streams can help you get to your data.

8: Use Traits in Java
Using traits can vastly help simplify your code and allow you to reuse components with ease. This example examines using traits with interfaces.

9:Event Sourcing
Event sourcing opens up a new way of looking at your database apps. Turn your database into a record of transactions that you can look back to and restart from.

10: MapStream
The open-source class MapStream lets you stream over elements as well as pairs of key value elements, making changes to everything along the way.

11: Try Java FX
JavaFX offers a variety of features that help improve observability, configure various bindings, and combine with reactive libraries.

12: Avoid Overloading With Lambdas
Lambdas are fantastic, but their popularity might lead to sloppy use. Naming them after specific uses keeps both the compiler and the client happy.

13: Try Higher Order of Functionality
See a QuickSort algorithm in action as an example of Higher Order Functionality. Taking declarative programming to a new level opens new doors to abstraction.

14: Submitting a Task
These days, Java developers have a variety of means to execute tasks. From threads to join pools to caching, you have no shortage of options.

15: Don’t Optimize Now!
If you're going to use JVM, it's important to know what's going on under the hood. Knowing exactly how code flattening works can save you time to put to better use.

16: Hacking the Existing Java Classes
Get your black hat - it's time to hack some classes. But because everyone follows the rules, this tip will help squash third-party bugs and deal with non-standard classes.

17: Parallel Streams in Custom Thread Pools
Java 8's streams, in theory, make it easy to use parallelism. It doesn't always turn out that way, but you can create custom thread pools to expand your resources.

18: Easily Create Database Content
You can use Java and Speedment together to easily analyze your database and generate code and content for it as well.

19: Speed Up Your Enums
The .values() method might seem speedy, but it actually creates a copy of your array, adding to your overhead. Doing a bit of extra work yourself will help in the long run.

20: Break Out of the Java Heap
Moving data off the heap, within reason, can let you work more efficiently while keeping latency low. That way, you can avoid the garbage collection wall for longer.

21: Concatenate Java Streams
Merging your streams through concatenation can allow one stream to lazily consume others, saving you time and making your code more efficient.

22: Use Enums as Method Parameters
Using Enums as parameters can greatly enhance the versatility of your methods. This example shows off how Enums can be used to nimbly and simply sort your lists.

23: Use Mappable Types Instead of Bloated Ones
Using mappable types in your geometric classes means less inter-class coupling and opens up a more functional path -- where you can apply functions on objects.

24: How Many Santas Are There, Really?
Yes, Virginia, there really is a Santa Claus. In fact, there might be a lot of them. Let's do some Java math to see how many Santas it takes to deliver presents around the world.


Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season. I am contributing to open-source Speedment, a stream based ORM tool and runtime. Please check it out on GitHub.

Saturday, December 24, 2016

Day 24, Java Holiday Calendar 2016, How many Santas are there - Really?

Day 24, Java Holiday Calendar 2016, How many Santas are there - Really?



Today we are going to do some nonsense calculations. Is there a Santa and if yes, how many Stantas can we expect to find in the world?

The Assumptions


Let us start with some assumptions. When this article was written there was an estimate of 7,472,085,518 humans on Earth. Let us disregard the guys that lives on the International Space Station, because presumably, Santa's reindeers cannot operate in near-earth-orbit. Luckily for those guys, Elon Musk can deliver their presents via Space X's delivery rocket instead.

A large number of the remaining people are not visited by Santa at all including people of various religious affiliations or beliefs and people that are very poor. We assume that only one third of the remaining persons are visited. We further assume that in average, there are three persons per household (this is not unreasonable taking the vast number of single-person-households into account). Furthermore, we assume that each person gets four presents in average (some rich ones get much more, some poor just get one if they are lucky).

Additionally we estimate that there is an average distance of 500 m (i.e. 0.5 km or 0.3 miles) between each household and that Santa's average sleigh speed is 4 m/s (i.e. 14 km/h or 9 mph) and that it takes Santa 1 second to drop of each present. It should be noted that the actual procedure of handing over presents differs from country to country. In the US, Santa is more likely to drop the packages down the chimney and curve the packages so that they always backspin and land in socks that are hanged near the beginning of the chimney whereas in some European countries, he is more likely to deliver the packages in person. This is note covered in the model though.

Because the speed of the sleigh is not so high, Santa cannot take advantage of the fact that the Earth is rotating and the different time zones. So, Santa can only work 24 hours.

The Java Code 


In Java we can model the problem like this:

public class NumberOfSantas {

    private static final long NUMBER_OF_PERSONS = 7_472_085_518L / 3;
    private static final int PERSONS_PER_HOUSHOLD = 3;
    private static final long NUMBER_OF_HOUSHOLDS = NUMBER_OF_PERSONS / PERSONS_PER_HOUSHOLD;
    private static final int PRESENTS_PER_PERSON = 4;

    private static final int AVERAGE_DISTANCE_BETWEEN_HOUSHOLDS = 500; // m (i.e. 0.5 km or 0.3 miles)
    private static final int AVERAGE_SLEIGH_SPEED = 4; // 4 m/s (i.e. 14 km/h or 9 mph)
    private static final int TIME_TO_DELIVER_PRESENTS = 1; // 1 s 

    private static final int WORKING_HOURS = 24;

    public static void main(String[] args) {

        long totalTimeS = NUMBER_OF_HOUSHOLDS
            * (AVERAGE_DISTANCE_BETWEEN_HOUSHOLDS / AVERAGE_SLEIGH_SPEED)
            + TIME_TO_DELIVER_PRESENTS * NUMBER_OF_PERSONS * PRESENTS_PER_PERSON;

        long noSantas = totalTimeS / (WORKING_HOURS * 3600);

        System.out.format("There are %,d Santas in the world", noSantas);

    }

}

Evidently, there is a large number of Santas out there because the program above outputs the following:

There are 1,316,455 Santas in the world

(Ho, ho)^1.3 M...

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season. I am contributing to open-source Speedment, a stream based ORM tool and runtime. Please check it out on GitHub.