Category: Java core

How to iterate a List in Java

This tutorial shows several ways for iterating a List in Java. 1- Typical For loop Using a typical For loop, you can iterate a List as the following:

2- Enhanced For loop Since JDK 5.0, you can iterate a List using an enhanced For loop which works as the following:

3- Typical While loop Using a typical while...

ArrayList vs Vector

ArrayList and Vector collections are used interchangeably for storing several items with the same type inside the memory. In this tutorial, we list the major similarities and differences between these 2 collections. 1- Similarities Both collections implement List interface, hence they expose similar operations for add, edit and delete operations. Both store their items in a dynamic Array inside the memory. Both...

Hashtable VS HashMap

Hashing objects like HashMap and Hashtable are mostly used for storing and caching data as a key/value pairs inside the memory. In this tutorial, we list the major similarities and differences between these 2 data types. 1- Similarities Both expose the same interface for adding, removing and accessing elements from the memory. get(): retrieve an item by a key. remove():...

Java – Convert File to byte[]

This tutorial shows several ways to convert a File object to a byte[] array in Java. 1- Traditional way The traditional conversion way is through using read() method of InputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can...

Java – Convert byte[] to File

This tutorial shows several ways to convert a byte[] array to File in Java. 1- Traditional way The traditional way of doing the conversion is through using FileOutputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can do...

Convert JSON to Java Object

This tutorials shows 2 ways for converting a JSON string to a Java object. 1- Gson Gson is the most popular library for converting JSON string to Java objects. With Gson, you can do the conversion in one line as the following:

Some points to be considered when using Gson: If the JSON string holds an invalid object attribute, then Gson implicitly...

Convert Java object to JSON

This tutorial shows 2 ways for converting Java objects to JSON. This kind of conversion is normally done via third-party libraries as it’s not supported by the JDK itself and requires a hard work to do it manually. 1- Gson The most popular library used for converting Java objects to JSON is the Google Gson library. Using Gson, you can...

Java – Convert List to comma-separated String

This tutorial shows several ways for converting a List to a comma-separated String in Java. 1- Java 7 The traditional way of converting a List to a comma-separated String in Java 7 and older versions is through using StringBuilder as the following:

2- Java 8 In Java 8, there are 2 conversion ways. 2.1. String.join() The most common way is...

Java – Convert comma-separated String to List

This tutorial shows several ways for converting a comma-separated String to a List in Java. 1- Java 7 With Java 7 and older versions, the typical way of converting a comma-separated String to a List is through splitting the String by the comma “,” delimiter and then generating a List using Arrays.asList() as the following:

2- Java 8 In...

Java – Convert InputStream to String

This tutorial shows several ways to convert an InputStream to a String in Java. 1- BufferedReader The common way for generating a String out of an InputStream is through using BufferedReader as the following:

2- Scanner Another common way is to use a Scanner object as the following:

3- Java 8 With Java 8, you can generate a...