Categories
Java

How to map and filter a list with Java lambda expressions

In this short tutorial, we will see how to map and filter a list with Java lambdas. 1) Map a list Let’s start from a list of integers: List<Integer> list = List.of(1, 3, 6, 2, 4, 5); To map the list, we need to convert it into a stream, then map it and finally collect […]

Categories
Java SQL

Create a PostgreSQL index on a column and test performance with Java JDBC

In this tutorial we will see how to create an index on a SQL table column with JDBC, then we will fill a table with 10⁷ rows (using PostgreSQL fast COPY function), and finally we will compare the select performance with vs without the index. 1) The SQL tables and the index We will create […]

Categories
Java SQL

Real life useful example of an Inner Join with Java JDBC and PostgreSQL

In this tutorial we will run a full demo of a real life useful Inner Join, with Java JDBC and PostgreSQL. Let’s consider a recurring business that has Customers, Products to sell to customers and Subscriptions that give the list of products each customer has subscribed to. What we want to do is store all […]

Categories
Java

Parse HTML in Java with XPath and Jsoup

In this tutorial, we will explain how to parse and extract content from an HTML source code. First we will download a real HTML source code with Apache HTTP client and then we will parse it with an awesome Java library called Xsoup. It is a mix of Jsoup and XPath. It is better adapted […]

Categories
Java SQL

Insert a Java object into a SQL table, with automatic mapping

In this tutorial, we will see a simple method to insert a Java object as a row into a SQL table. Thanks to Apache DB Utils and Apache Bean Utils, our mapping between Java JDBC and SQL will be automatic. 1) The Java object Let’s consider the following Java class Customer: (@AllArgsConstructor and @Data are annotations from the Lombok […]

Categories
Docker Java SQL

Run PostgreSQL with Java and Testcontainers

In this tutorial, we will explain how to run PostgreSQL with Java and Testcontainers. Testcontainers is an awesome library that allows you to configure and start docker containers from Java. To run a PostgreSQL database, you need Docker on your machine. Add these dependencies to your pom.xml: Then, simply run: Replace databaseName, username and password […]

Categories
Java SQL

Map JDBC result set to Java list of objects in 2 lines

In this tutorial we will see how to very simply transform a JDBC result set into a list of Java objects. Let’s say you have the following SQL table customer: On the Java side, you have the following POJO called Customer: @NoArgsConstructor, @AllArgsConstructor and @Data are annotations from the Lombok project. They add constructors, getters and setters to the […]

Categories
Java

Sort a list of objects in Java, in 1 line of code with method reference

In this tutorial, we will explain how to sort a list of Java objects based on a field of the object. Firstly, we will see a method using the method reference operator :: that takes only 1 line of code. Then we will see a more generic method based on a lambda comparator that needs […]

Categories
Java

Extract text between two strings with Java regex

Java is a general purpose language that has a library for almost anything. Using the Java Regex library, you can easily extract the text between two predefined strings. To extract the text between two strings tag1 and tag2 from the string content, we only need 4 lines of code: You can print the variable extracted […]

Categories
Java SQL

Limit PostgreSQL table size with a trigger and Java JDBC

In this tutorial, we will explain how to easily limit the size of a PostgreSQL table (by size we mean number of rows). To achieve this, we will create a trigger on the table via Java JDBC. The trigger will be atomically executed after each SQL insert, and the insert that causes the trigger to […]