Dealing with date and time is a cumbersome task in many programming languages. But with Java 8, the JDK provides us with a comprehensive and completely new API, changing the way we deal with time-related concepts.
Even though JSR310 was introduced with Java 8, the code example will use a Java 10 feature, local variable type inference, to improve readability.
The code examples themselves will all be Java 8 compatible, though.
// => part of code examples will show the
toString() output of the previous line/variable.
The three methods,
reduce, are the cornerstone of any functional programming.
Usually, our data pipelines consist of one or more intermediate operations, transforming (aka mapping) and/or filtering elements, and a terminal operation to gather the data again (aka reducing).
With just these three, we can do a lot, so it’s worth knowing them intimately. But they have some close relatives that can be useful, too.
Note: The article assumes Java 9. Method signatures and visibility modifiers are shortened for readability.
Java is often criticized as being too verbose. One aspect contributing to this characterization is the requirement to specify every type explicitly, which leads to a lot of additional noise.
A new way of declaring local variables with less clutter was given to us with JDK 10 and JEP 286: local variable type inference.
The name itself perfectly describes the feature’s core and its limitations:
The Java compiler will automatically detect the correct type for us. …
java.util.Locale to format dates, numbers, currency, and more. But in some circumstances, these formatted strings have changed with JDK 9, leading to a multitude of subtle (and sometimes not so subtle) bugs.
To better understand a problem, we must first know what we’re dealing with.
“A locale is a set of parameters that defines the user`s language, region, and any special variant preferences that the user wants to see in her user interface ”— Wikipedia
The core use of a locale is formatting many kinds of different data for output…
I’m a great proponent of building our own tools. As developers, we perform many repetitive tasks, big and small. Many of these tasks might be made easier by using an appropriate tool instead.
We often concentrate too much on solving more significant problems. But sometimes it doesn’t need to be a real problem for a tool to be valuable. We have to find the sweet spots in our workflow.
The best investment is in the tools of one’s own trade
– Benjamin Franklin
This is the story of why and how I built a multi-repository Git CLI helper tool, Tortuga, and what I’ve learned doing it. …
Shell scripting is a powerful tool available on all platforms, even Windows, thanks to WSL. But it can be easy to make mistakes. Here are some tips to improve our scripts and avoid many problems.
TABLE OF CONTENTS#1: Better Shell Options
#2: It's a TRAP
#3: Check Requirements
#4: Temporary Files & Directories
#5: Quoting (almost) everything
#6: Linting with ShellCheck
#7: Doin' it in Style
#8: Targeting the Right Shell
#9: Don't Use Shell Script
All shells have configurable options, which can be used to enable behavior. Many of them are considered safer than the shell defaults.
An absolute no-brainer is the option
set -e. …
In object-oriented languages, a nested or inner class is a class that’s completely declared within another class.
This allows us to combine classes that are logically bound together, to increase encapsulation, for more concise and maintainable code.
Here’s a quick, non-deep-dive overview of the 4 types of nested classes.
TABLE OF CONTENTS- Static Nested Classes
- Non-Static Classes / Inner Classes
- Local Classes
- Anonymous Classes
A nested class is defined like any other class:
static member, a
static nested class is bound to the class itself, and not an instance of it. This means we can instantiate it without creating an intermediate instance of
Even in my article about Java exceptions, I didn’t write about how to handle them in lambdas and streams because it deserved its own article.
Almost every developer knows the phrase “premature optimization is the root of all evil”, coined by Donald Knuth in 1974. But how are we supposed to know what is worthy of being optimized?
Our computing power improved manifold since then. But the sentiment to focus on the real issues for optimization efforts holds still true. Understanding the different kinds of latency and how to find actual relevant bottlenecks, and not only perceived ones, is the key to good benchmarking.
It’s an axiom of business management, named after the Italian economists Vilfredo Pareto, stating
80% of sales come from 20% of clients. …
Every time we create a new data structure, we have to decide which data types to use. Usually, the decision is simple: text most likely will become a
String, non-floating-point numbers will be
int, and so forth.
Most of the time, these almost subconsciously-made decisions will suffice. But to design future-proof data structures, we have to think about choosing the correct data type a little more.
Even though the definition of “right” is highly subjective, the right data type depends on 4 different, interconnected factors: