Check if value is present as key in a Map in Java

This example shows how to check if a value exists as a key in a map. Let’s say we have a ComboBox and we want to compare the selected value in the ComboBox with they keys in a map to see if it exists.

There are a few different options to do just that and the example below will show a few of them. Let’s assume we have a ComboBox with a corresponding model called comboBoxModel.

In the example above the method keySet is called on the coordinates to get a Collection (a Set) which has a method called contains where we just send in our value to look for. The method returns true if it exists, otherwise false.

In the example above we check if value exists as key in the Hashtable by using lambda expressions which is available from Java version 1.8.
We get the Stream from the keySet and filter out values that matches the selected value and call the findFirst() method. In this case it should only be one item that matches the value so we could also call the findAny() method. Both of those methods return an Optional object whether the value is found or not. Then call isPresent() on the object to check if the value is actually found. If so, call get() on it to retrieve the value.

If you don’t want to use an optional object you can instread call orElse() method and specify what value should be returned if not found. In the example above we just return null, and the if statement just do a null-check on the variable returned.

Divide a list into sublists in Java

Sometimes you have large lists that you need to divide into sublists due to different reasons. One reason could be that you need to make a query to a database with all the values in an IN-clause, but the database has a limit to how many elements you can provide in that case. Then you might want to make several IN-clauses separated by OR. In such a case you need to divide your list into sublists and this example shows one way to do it.

This example uses generics so the list can contain any type of objects. In this case we use a list of Long-values. We create a list using the Random class to randomly generate 3005 values to add to the list.
Then we call the toSublists method passing the list we just created along with a max size value of each sublist. In this case we have a static final variable called SUBLIST_MAX_SIZE that is set to 1000.

The return value from that method is a List where each element is a sublist of the original list. Finally we just print out the length of each sublist. Below is the code just explained:

The output from the code above will show that 3 sublists contains the max value of 1000 elements and the final sublist will contain 5 elements.

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

Convert Map to List in Java

This code example shows how to convert a Map-object to a List-object.

A Map consist of key and value pairs but every element in a List contains only one value. So either we have to convert every key item to a List item, every value item to a List item or a combination of key- and value items to a List item.

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