How to initialize a static map in Java

This example shows how to initialize a static map. It actually contains two examples, the first one can be used with any version of Java and the second one can be used from Java 8 and forward.

In the example below we initialize two static maps in two different ways. The first map is initialized with a static code block, or a static initializer. The second static map is initialized using an instance initializer. Finally the contents of both static maps are printed out in the main method.

The output from the code above is:

Using Java 8 features to initialize a static map

The second example also initialize a static map, but with lambda expressions:

The output from the code above:

Convert list from one type to another using lambda expressions in Java

This example shows how to convert a list of a certain type of objects to a list with another type of objects using lambda expressions (available from Java 8).

First we create to classes to work with, Employee and Person. The employee has got three attributes and the Person class has just one that matches one the attributes from Employee, the name.

Then we create an ArrayList of employee objects and add one object to it. Finally we do the conversion like this:

Create a List of a specific type from an array of the same specific type

This example shows how to create a List which holds objects of a specific type from an array which holds elements of the same type. Instead of converting to a List you could as easily convert to any type of list, like ArrayList, as long as it implements the List interface. Or more generally, we could say that we convert T[] to List, where the letter T represents Type.

In the example below 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

Convert LinkedList to Array

This code example shows how to convert a LinkedList containing Strings to an array of Strings.
We begin by creating and populating the LinkedList with String representing different fruits. The actual conversion is done by calling the toArray() method of the list which returns the array. As input we create an empty String-array to tell the method to which type the elements should be converted.

Continue reading