Generics

Generics are a great way to prevent bugs at runtime since they provide compile-time type checking and promote cast elimination. Another important advantage of using generics is that you can write code that uses abstractions instead of concrete classes, so you can create maintainable and extendable code. The following example shows how we can print a collection containing different types using generics: import java.util.List; import java.util.Arrays; public class ListPrinter { private <T> void printList(List<T> collection){ for(T item: collection){ System. [Read More]

Executors

Executors are part of concurrent Java API, and the intention is to have a high-level managing thread; therefore, executors typically manage a pool of threads. ExecutorService executor = Executors.newFixedThreadPool(3); In the previous code, we created a thread pool with three threads of capacity. It would be best if you stopped executors at some point; otherwise, they will keep listening for new tasks; the usual way to stop it is: executor.shutdown(); Here is a simple executor example: [Read More]

Stream Collectors

This time, I will show you how to use collectors along with streams to group by, concatenate, map, and list. Example: Convert a list of elements to a string separated by ‘,’ import java.util.List; import java.util.stream.Collectors; public class StreamToStringConverter { private String parse(){ return List.of("Java", "C++", "Lisp", "Haskell"). stream().collect(Collectors.joining(",")); } public static void main(String[] args){ var result = new StreamToStringConverter().parse(); assert "Java,C++,Lisp,Haskell".equals(result); } } From a list, generate another list selecting elements if the string length equals four. [Read More]

Stream Filters

A stream represents a sequence of elements and supports different operations to perform calculations in those elements: import java.util.List; import java.util.Arrays; import java.util.stream.Collectors; public class StringFilter { private List<String> parse(){ return Arrays.asList("Java", "C++", "Lisp", "Haskell"). stream().filter( p -> p.startsWith("J")).toList(); } public static void main(String[] args){ var result = new StringFilter().parse(); assert 1 == result.size(); assert "Java" == result.get(0); } } After creating a Stream of Strings, we filtered by those starting with the letter J. [Read More]

Streams

The stream interface is defined in the java.util.stream package. Since Java 8, the stream API will be used to process collections. Streams support aggregate operations. The common aggregate operations are filter, map, reduce, find, match, and sort. The map method maps each element to its corresponding result. The following Java code uses the map to get squares of numbers. import java.util.List; import java.util.Arrays; import java.util.ArrayList; import java.util.stream.Collectors; public class MapSquares { private List<Integer> square(List<Integer> numbers){ return numbers. [Read More]

Java

There are three main features in Java 8 that I love, one of them is the lambda expressions, it will have enormous implications for simplifying development. The another one is the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations. And lastly but not least the java. [Read More]

Java Time API

Some important things that we have to know about the java.time API is the following: Is an evolution from the previous java.util.Date (Calendar, TimeZone & DateFormat) Instances of time/date now are immutable. (This is importat for lambda expressions) Time and Date operations now are thread safe. The API support strong typing, which enables you to write better code. A quick glance to the Date methods import java.time.*; public class DateOperations { private void operations() throws Exception { LocalDate localDate = LocalDate. [Read More]