Category Archives for Code Samples

Array List add at index Java Example

ArrayList is a dynamic array. It means it can grow as you add items in to it. If required, you can convert array in to ArrayList easily. There are time, the programmer wants to add an item at certain index. This is possible in ArrayList.

ArrayList has an overload add method that takes index. The signature would be ArrayList.add(index, Object).

When you add an object at an index, the item currently at the position gets shifted to accommodate this change. This is shown in diagram as below.

ArrayList with three items

Java ArrayList Before Add

The same ArrayList after, ‘NewItem’ has been added

Java ArrayList After Add

Here is code example for that.

If you all this function from a method, here will be the output:

 

ArrayList Object Sort in Java with Comparable and Comparator

Sorting ArrayList that contains custom objects need a different approach than, a ArrayList with primitive types. Sorting ArrayList with basic types is easy.

When it has custom objects, we need to specify what you really mean by sorting. For example, a User object might have properties age and name. When you are sorting on Users, do you want to sort by age or name?

This is why we need to have our own way to mention the sorting mechanism.

Two approaches could be taken here.

Sort with Comparable Interface

If you can implement Comparable interface on the Class to be sorted, it will force you to have compareTo() method. Here is where you mention your sorting mechanism.

The following code shows sorting with Comparable Interface. We are sorting objects of User class.

User class:

Main:

Output:

See the result sorted by age.

 

Sort with Comparator

So why we need Comparator? If you look in to the Comparable mechanism, you might have noticed that you can do it only for a single property and that has to be an integer.

With Comparator we have even more flexibility and we can compare based on various properties.

In the following example, we will sort User objects by age and name.

Approach is to create two comparators one for age and another for name. In the Collections.sort, we will pass appropriate Comparator.

User class:

Main:

 

Output:

 

ArrayList with different Object Types

Keeping different types of objects in the same ArrayList object could be done by using Object generic type.

The idea here is to allow ArrayList to take any Object then check the actual type at the time of retrieving it.

The following code explains this:

Output:

Similar code samples:

Sorting ArrayList of primitive types

Create java.sql.Date for current Date

Here is the code to create java.sql.Date for the current timestamp.

Approach:

Create util Date (java.util.Date) with new operator.

Get the time in long, then create a java.sql.Date using that value.

Similar approach is used in conversion of util Date to sql Date.

Code:

 

Convert java.util.Date to java.time.LocaleDate

Here is the code sample to convert  java.util.Date to java.time.LocaleDate

 

 

1 2 3