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

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

Convert a List (ArrayList) to an Array

This piece of Java code shows how to convert a List, in this case an ArrayList, to an Array by calling the method toArray() on the List object.
The toArray() method returns an array of the same type as the array provided as an argument to the method. The argument can be an empty array as in the example below.

Continue reading

Convert an Array to a List

This piece of Java code shows how to convert an array to one of the Colletion types, namely a List.
There is a class called Arrays (not to be confused with an array) that has a method called asList() which takes an array as argument and converts it to a List object.
Generics can be used to specify the List elements type.

Continue reading

Convert a List to a Set (ArrayList to HashSet)

This example shows how to convert from an ArrayList to a HashSet. Since most Collection objects have a constructor that allows for passing in another Collection object we are able to make these kinds of conversions easily.
In the example we create an ArrayList to which we add a few string items. Next we create a HashSet and provide the reference to the ArrayList as argument to the HashSet constructor.
Finally we just loop through the HashSet and print out the contents.

Continue reading

Convert a Queue to a List (LinkedList to ArrayList)

This example shows how to do a Queue to List conversion. Since both Queue and List are of type Collection we can create any of them using the constructor that takes another Collection object as argument.
Here we create a LinkedList and add items to it, then we create an ArrayList and pass the LinkedList as an argument. Finally we print out the contents of the newly created ArrayList.

Continue reading