List possible Time Zones or ZoneIds in Java

Sometimes you might need to get the date and time for a specific time zone. A specific case would be when you need to pass in the ZoneId to one of the classes LocalDate, LocalTime or LocalDateTime. This code example shows which ZoneIds that can be used by fetching them from the system and printing them out.

Continue reading

How to sort an array in Java using bubble sort

Normally when sorting an array in Java the convenient method sort() of the Arrays class is used. If this was what you where looking for, follow this link to:
How to sort an array

This example shows how to sort an array without using that method, just for better understanding on how bubble sort works. Two different scenarios are used here, one where we sort an array of int values and one where we sort an array of objects.
This first example shows how to use bubble sort to sort integer values in ascending order.

Continue reading

Find duplicate elements in an array

This Java code example shows how to find duplicates in an array. For this example we are using an int[] array as our input to process.
Every number in the array is stored in a Map collection object as the key, along with a value that tells how many times that particular number exist in the array. This is done by looping through the array and checking the map for that number. If it exists in the map the value is incremented by one. If not it is added to the map with a value of 1.

Continue reading

Sort rows in file based upon a value on each row

Here’s an example that can solve the problem to sort rows in a file based upon a certain value on each row.
Say for example that we have a text file which has information about employees. The first value is employee nr, the second is the name of the employee and the third value is the salary.
The values on each row are separated by a comma. In this code example we are going to sort the list ascending on the salary value.

This is what the file ’employees.txt’ looks like:

Continue reading

Sorting an Array in Descending (Reverse) Order

The Arrays class can be used to sort an array of either reference types or primitive types, but the Arrays class does have an overloaded sort method that can sort an array in any way we choose. That overloaded variant of the sort method can only take reference types, and it has an additional parameter which is of type Comparator. This is the parameter that tells how we want the array sorted. In the example below we call the static method reverseOrder on the Collections class. It returns a Comparator instance which will sort the array descending instead of normal ascending.
Since this overloaded sort method only take reference types, we have to use the proper wrapper class (in this case Integer) if we want to work with primitive types.

Continue reading

Using the Comparable interface to compare and sort objects

This code example shows how to compare and sort objects by implementing the Comparable interface. The code consists of a class called Main and another class called Car.
The Main class creates a few instances of the Car class (which is the one that implements the Comparable interface) and them compare them and also sort them.

The Car class has three attributes, make, year and mileage. Only the mileage attribute is really of importance here since it’s the attribute used for comparing.
When a class implements the Comparable interface it has to implement a method called compareTo(). In that method the code has to be written to decide if two objects match or if one is larger than the other.

Continue reading

How to sort an array

A common mistake for a beginner to Java is to try and write some more or less complex method for sorting an array when in fact it can be done with just one line of code.
The Arrays class has a static method called sort which exists in many overloaded versions so you can pass in an array of any primitive type, or pass in an array of Strings or other Objects.
Since it is the reference of the array that is passed to the sort method nothing needs to be returned from it, the array is altered through the reference.
The example below shows two nearly identical methods, one that sorts an int array, and one that sorts an array of Strings.

Continue reading