Posts

Showing posts from February, 2021

JavaFX 15 Installation and Introduction

Image
From the openjfx site   JavaFX allows you to create Java applications with a modern, hardware-accelerated user interface that is highly portable. Creating an user interface is one of the integral part of any programming language. For a long time I have searched for a alternatives to Swing and initial Javafx to create a user interface that was easier to code and involved easier integration.  For some time I was thinking of using electron to build desktop application and leverage the complex angularjs based framework for the same. Since, I know a bit of python, I also thought of using Python based application as well. But, Java has been so far suiting what most of what I do. Again, I am hobbyist and creating Desktop Applications has nothing to with what I have been doing so far in my coding life. I have been using javafx2 for most of my needs, but JavaFX15 has been a real game turner here for me. It is faster in terms of integration etc. So far I have been trying to get my hands...

Comparing Objects in Java - Lambdas

Sorting has  come a log way since Java was created. Techniques have changed. There was huge change when JDK1.5 was launched. JDK1.7 involved a somewhat more changes but JDK1.8 had considerable changes like lambdas. Lambdas not only gave a new perspective for one liner functions or anonymous classes but also eased a lot of coding complexities or reduced Lines of code. With that said, sorting a list of objects has been a lot more comfortable as well. We have already talked about how we sort using Comparators and Comparable. You can visit that discussion here . Let's say you want to sort a list. Without Lambdas you'll create a comparator and pass it to the sort method of Collections class. But, before we do that, Let's create a Product class that has the properties of a product and regular Getter and Setters. You can download the complete class from github . If you are downloading the code from github, then it has some Jackson related annotations. If you are using your own p...

Comparing Objects in Java - Comparators and Comparable

Image
Creating our own classes that represent real world entities are a basic coding task nowadays. For example, a project dealing with ecommerce apps will need to create Product, Category, Cart etc. kinds on user defined classes. A finance app will contain Fund, Account etc. classes.  All these classes will contain information related to the entity they represent. As an example, we can build a Product class that may have information like product Id, product name, category, sub products, price, created by etc.  These user-defined classes are objects, that are created with information or data about the instance itself. Objects are often combined or grouped together as per the requirement. For example, objects of Product class can be grouped in an Array or ArrayList. Now, imagine you want to see the list of products. Simple, iterate over the list and print the product values. However, user wants to see the list in order of ascending order of names or more realistically, in order of pr...

LocalDate - Java

Image
We have been using java.util.Date for a very long time. There have been times when the basic Date class was not sufficient enough and hence, for my complex project related requirements I often used Joda  or  Calendar classes etc. for the same. While the inbuilt Date, Calendar etc. classes were provided, I used to write a lot of code for complicated tasks. Using third party libraries was always an option but I am not a very string advocate of using third party libraries until it is absolutely required and save my coding and testing time by a huge margin.  One of the reason when I came across java.time.LocalDate() in JDK8, I was very excited. Coming to point straightaway, LocalDate object carries information about date, namely year, month, date. It has operations that help us do certain modifications on date. If we quote java documentation LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-yea...

Insertion Sort

Image
Sorting has been a very basic need in the world of programming. Insertion sort, for me is a very basic sorting technique. For anyone who plays cards, this is quite simple. Imagine, you are picking card one by one from right hand, and placing them in the correct position in your left hand. This is the very basis of this technique.  Lets start by defining a pseudo code. Psuedo Code for j = 2 to A:length key = A[j] i = j - 1 while i > 0 and A[i] > key A[i + 1] = A[i] i = i - 1 A[i + 1] = key Explanation The above process explains the concept in detail. We always start with the second number and consider it as a key. After every iteration of the loop, the left hand side of the key should be sorted and right hand is the unsorted list. Now, for each iteration, our goal is to put the key to its correct position on the left.  let's compare the key with the element on the left. If the key is smaller then the position of key needs to be changed. We'll move con...