How to convert List to Byte Array in Java? – Easy way

In this post, We will see how to convert List to Byte Array in Java using a simple approach. We will use just two predefined classes to write this program.

How to convert List to Byte Array in Java?

We can convert List to Byte Array in Java by using two predefined classes as  ByteArrayOutputStream class and ObjectOutputStream

Finally, by using the toByteArray( ) method, we will convert our List to Byte Array. 

Let’s see the program now.

/**
 * How to convert list to byte array in Java?
 */

package cf.java.array;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ListToByteArray {

	public static void main(String[] args) {
		// Declare all variables
		List<String> list;
		byte[] byteArray;
		
		// Create a String array list
		list = new ArrayList<String>();
		// Add elements to list
		list.add("Codingface");
		list.add("java");
		list.add("list");
		list.add("array");
		// Display given Array List
		System.out.println("Given Array List: "+ list);
		/**
		 * This class implements an output stream in which the data 
		 * is written into a byte array. The buffer automatically 
		 * grows as data is written to it.
		 * reference: Java Docs
		 */
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		/**
		 * An ObjectOutputStream writes primitive data types and 
		 * graphs of Java objects to an OutputStream. The objects can be 
		 * read (reconstituted) using anObjectInputStream. Persistent storage 
		 * of objects can be accomplished by using a file for the stream.
		 * reference: Java Docs
		 */
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(list);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// Convert to Byte Array
		byteArray = baos.toByteArray();
		System.out.println("List is converted to Byte Array successfully");
		}

}
Code language: PHP (php)

Output of Program

Given Array List: [Codingface, java, list, array]
List is converted to Byte Array successfullyCode language: PHP (php)

Must See: How To Check String Contains Special Characters In Java?

Explanation of Program

We have declared an ArrayList and added elements to it in the above program by using add( ) method.

We have taken 2 predefined classes ByteArrayOutputStream and ObjectOutputStream which are basically used to implement an out stream to write data into byte array and then read the data. You can also refer to the above program where we have explained with comments.

Finally, by using the toByteArray( ) method of ByteArrayOutputStream class, we have to convert it to Byte Array.

Other Queries We Have Covered

  1. How to Convert array list to single byte[]?
  2. How to convert List String to Byte Array in Java?
  3. How to convert a list to bytes?

FAQs

How to convert array list to byte array in Java?

We can convert ArrayList to Byte Array in Java by using two predefined classes as  ByteArrayOutputStream class and ObjectOutputStream
Check the complete program here.

What is ByteArrayOutputStream class?

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().
Closing a ByteArrayOutputStrea does not affect. The methods in this class can be called after the stream has been closed without generating an IOException.
Reference: Java Docs

Leave a Comment