Home » Author Archives: Fahd Shariff

Author Archives: Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.

Java 8: CompletableFuture vs Parallel Stream

java-logo

This post shows how Java 8’s CompletableFuture compares with parallel streams when peforming asynchronous computations. We will use the following class to model a long-running task: class MyTask { private final int duration; public MyTask(int duration) { this.duration = duration; } public int calculate() { System.out.println(Thread.currentThread().getName()); try { Thread.sleep(duration * 1000); } catch (final InterruptedException e) { throw new RuntimeException(e); ...

Read More »

Java 8: Default Method Resolution Rules

java-logo

With the introduction of default methods in Java 8, it is now possible for a class to inherit the same method from multiple places (such as another class or interface). The following rules can be used to determine which method is selected in such cases: A class or superclass method declaration always takes priority over a default method Otherwise, the ...

Read More »

Java 8: Converting Anonymous Classes to Lambda Expressions

java-logo

Refactoring anonymous classes (that implement one single method) to lambda expressions, makes your code more succint and readable. For example, here’s an anonymous class for a Runnable and its lambda equivalent: // using an anonymous class Runnable r = new Runnable() { @Override public void run() { System.out.println("Hello"); } }; // using a lambda expression Runnable r2 = () -> ...

Read More »

Java 8 Streams API: Grouping and Partitioning a Stream

java-logo

This post shows how you can use the Collectors available in the Streams API to group elements of a stream with groupingBy and partition elements of a stream with partitioningBy. Consider a stream of Employee objects, each with a name, city and number of sales, as shown in the table below: +----------+------------+-----------------+ | Name | City | Number of Sales ...

Read More »

Converting XML to CSV using XSLT 1.0

software-development-2-logo

This post shows you how to convert a simple XML file to CSV using XSLT. Consider the following sample XML:                 <library> <book> <author>Dan Simmons</author> <title>Hyperion</title> <publishDate>1989</publishDate> </book> <book> <author>Douglas Adams</author> <title>The Hitchhiker's Guide to the Galaxy</title> <publishDate>1979</publishDate> </book> </library> This is the desired CSV output: author,title,publishDate Dan Simmons,Hyperion,1989 Douglas Adams,The Hitchhiker's Guide ...

Read More »

Throttling Task Submission with a BlockingExecutor

java-logo

The JDK’s java.util.concurrent.ThreadPoolExecutor allows you to submit tasks to a thread pool and uses a BlockingQueue to hold submitted tasks. If you have thousands of tasks to submit, you specify a “bounded” queue (i.e. one with a maximum capacity) otherwise your JVM may run out of memory. You can set a RejectedExecutionHandler to handle what happens when the queue is ...

Read More »

Throttling Task Submission with a BlockingExecutor

java-logo

The JDK’s java.util.concurrent.ThreadPoolExecutor allows you to submit tasks to a thread pool and uses a BlockingQueue to hold submitted tasks. If you have thousands of tasks to submit, you specify a “bounded” queue (i.e. one with a maximum capacity) otherwise your JVM may run out of memory. You can set a RejectedExecutionHandler to handle what happens when the queue is ...

Read More »

Shell Scripting – Best Practices

devops-logo

Most programming languages have a set of “best practices” that should be followed when writing code in that language. However, I have not been able to find a comprehensive one for shell scripting so have decided to write my own based on my experience writing shell scripts over the years. A note on portability: Since I mainly write shell scripts ...

Read More »

Guava 15 – New features

java-logo

A new version of the Guava library was released earlier this month and contains several new features and improvements. Here is an overview of some of the significant API additions in this release: 1. Escapers Escapers allow you to “escape” special characters in a string in order to make the string conform to a particular format. For example, in XML, ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns