130. Implementing join algorithms
Join algorithms are typically used in databases, mainly when we have two tables in a one-to-many relationship and we want to fetch a result set containing this mapping based on a join predicate. In the following figure, we have the author
and book
tables. An author can have multiple books and we want to join these tables to obtain a result set as the third table.
Figure 5.47: Joining two tables (author and book)
There are three popular join algorithms for solving this problem: Nested Loop Join, Hash Join, and Sort Merge Join. While databases are optimized to choose the most appropriate join for the given query, let’s try to implement them in plain Java on the following two tables expressed as records:
public record Author(int authorId, String name) {}
public record Book(int bookId, String title, int authorId) {}
List<Author> authorsTable = Arrays.asList(
new Author(1, "Author_1"), new Author(2, "Author_2...