Posts

Showing posts from February, 2019

Java: How to work with streams

Filter a list and process each element Filter a list and collect the elements Transform a list Reduce a list

Java: How to use the Function interface

I'll show you how you can use the Function interface. The Function interface returns an Object of any type. The code that calls the createProductDescription uses lambda expression :

MySQL: Steps for creating a DB

Image
MySQL Installation First you need to install the MySQL server on your local environment. As soon as the installation is finished, the server should be up and running. MySQL WORKBENCH I use MySQL WORKBENCH extensively for creating and managing easily all my databases. You must install MySQL WORKBENCH on your computer if you've not done that yet. You can easily add a connection via this window : Create Schema Add a new Model (File/New Model) Edit the default schema (On Physical Schemas, right click ' mydb ') Change the schema name Add tables, views... Forward Engineer to Database We assume that your schema is ready to 'deploy' to your MySQL server. Click Database/Forward Engineer... Select your MySQL server Accept default options values Revue the SQL script to be executed Click next to continue The database should have been successfully created on your MySQL server. Adding administrator for the created database Open yo...

Java : Functional interfaces and Lamda expression

I'll show you how you can check a condition and return a boolean value. The java API includes 3 functional interfaces commonly used: Predicate<T>  Consumer<T> Function<T, R> The Predicate tests the T object and returns a boolean value. The Consumer accepts an operation on T but does not return a value. The Function performs an operation on T and returns an R object. The static method below uses the Predicate interface to specify the condition: You can use this method like this: Thanks

Using Stack to reverse a word

Using a Stack structure (LIFO) can be handy for reversing a word. I'll show you how you can do that with python:

Java : read and write to a binary file

Code for reading a binary file : Code for writing to a binary file : As said in a previous article, you should use buffered stream for improving I/O operations. It's also considered a best practice to use the try-with-resources for releasing resources.

Java : Read and Write to a file

The code below show you how you can read a file using Java: Code for Writing to a file: By using a buffered stream, you can dramatically improve the performance of I/O operations. We also use the try-with-resources for automatically closes the object and releases its resources.

GCP Datastore : Integration Testing using Spring Boot

In this article, I'll show you how you can create an in-memory datastore used by your tests. Configuring a Datastore Profile Testing Spring 3.1 introduced the bean definition profiles which can help us implementing an appropriate configuration for testing datastore staff. So, we'll have a specific profile for the GCP Datastore environment. Basically, the GCPDatastoreTester() class helps us saving data to the local in-memory datastore. Custom Annotation In order to support the population of test data for each test case, we will create an annotation in which you can add the JSON file name as argument. In the test execution listener, we'll check for the existence of the annotation and load the data accordingly. We'll use the annotation as follow : Implementing Custom TestExecutionListener The TestExecutionListener interface in the spring-test module defines a listener API for intercepting the events of the test case execution.  Process : ...

Simple Sorting : Insertion Sort

The insertion sort is better than the bubble sort and the selection sort. Process For a better understanding, we will describe the process from the middle. Outer loop : Record the current value (e.g. index 6) as temp (The values at the left are partially sorted). Inner loop : Start comparison to the left For each Inner pass : [If inner - 1 > temp] then [move inner - 1 to inner] else [move temp to inner] Outer loop : move to the right (e.g. index 7) continue above steps until end of outer loop Java: Python: The insertion sort runs in O(N 2 ) time. As a bonus, you can see below a version which removes duplicate:

Java : How to work with file and directory

The java.nio.file package (from Java 7) has improved the way we work with the file system. Create a directory For creating a new directory, we get a Path object for the provided path. Then, we check if the directory exists. If not, we call the static createDirectories() method of the Files class. Create a File For creating a file, we follow basically the same process. But we get a Path object that refers to the directory and file. If the file doesn't exist, we call createFile() method of the Files class. Iterate through Files in a directory By using the static newDirectoryStream() method of the Files class, you can list the files in a directory this way:

Simple Sorting: Selection Sort

The selection sort is a little bit more involved than the bubble sort. This algorithm basically makes a pass through all the values for picking the shortest one. This shortest value is swapped with the leftmost value on each pass. The sorted values accumulate on the left (in the bubble sort they accumulated on the right). Process Start on the left (outer loop) Record first index as minimum value by default Compare each value on the right (inner loop) If a shortest value exists, replace the minimum index with the current one. Swap outer value with the minimum Repeat above steps until end of outer loop Java: Python The selection sort and bubble sort have the same number of comparisons [N * (N-1)/2]. However, the selection sort is faster because there are fewer swaps. The selection sort runs in O(N 2 ) time .

Simple Sorting : Bubble sort

Hi, Today, I'll begin a very long journey of exploring algorithms that you need to know as a software engineer. I'll do my best to describe each and every one simply by providing you sample code in java and python. Each subsequent post will describe one and only one algorithm. We start by the most simple of them : the Bubble Sort . The bubble sort is notoriously slow. Process We start at the left of the array, we compare values in positions 0 and 1 If the value at index 0 is bigger, swap them Move on the right When you reach the end of the first pass, you know that the last value is sorted, start over at the left of the array till N-x (the number of pass) Java: Python: The bubble sort runs in O(N 2 ) time.