Java 8 StringJoiner demo

Finally Java has convenient and intuitive API for joining strings with delimiters! Since Java 8 there is StringJoiner class. It is an API that you may know from Guava Joiner classes (see my post: https://looksok.wordpress.com/2015/10/17/guava-joiner-join-all-strings-in-an-array-or-map/). Here is a short StringJoiner demo. Basic String joins The most basic usage is to create StringJoiner instance with delimiter … Continue reading Java 8 StringJoiner demo

Java 7: Try-With-Resources block that closes the resources for you

Working with resources (like files stream, byte stream, network stream) in Java requires to close() the resource after you're done. How often did you forget to close it in your finally block? Even if you didn't - Java creators proved that 75% of close() implementations got bugs in it. Since Java 7 all resources you … Continue reading Java 7: Try-With-Resources block that closes the resources for you

Java 8 streams demo part 2: Stream of custom objects

Following the List with primitive types like in post here: Java 8 Streams demo, now it's time to demonstrate streams with custom objects. Please read mentioned post first to get the idea of this one faster. Introduction Now I want to have a list of Users. User is a POJO having a name and age. … Continue reading Java 8 streams demo part 2: Stream of custom objects

Java 8 Streams demo

Why streams? Nearly every Java app iterates collections, making operations on its elements, searching, changing them, sorting and so on. Streams are more readable way of doing this. Streams operations are to some excent similar to SQL Query operations - you can select, filter, order items in collection. What is a stream Stream is a … Continue reading Java 8 Streams demo