How to Convert arrays into list

java.util.Arrays has asList() method which convert arrays into List. This method can convert any type of arrays into a list.

Method Declaration:

public static List<T> asList(T ...a)

Note : Notice here its argument and return type. It's generic return type and argument. So, for different object type, you would not need to type cast the return type.

Method1 : Demo Example to Illustrate how to convert arrays into list using java.util.Arrays class:

ConverArraysIntoListDemo.java

package com.mathapachi;

import java.util.Arrays;
import java.util.List;

public class ConverArraysIntoListDemo {

	public static void main(String[] args) {
   
		System.out.println("Demo Program to convert arrays into list");
		String arr[] = {"Ram","Deepak","Divya","Google"};
		List<String> asList = Arrays.asList(arr); 
		System.out.println("Converted List Output1 = "+asList); 
		
		Integer arr1[] = {1000,2000,28376,938478};
		List<Integer> asList1 = Arrays.asList(arr1); 
		System.out.println("Converted List Output2 = "+asList1); 
		
		
	}

}

Output of the above code is as follows:

Demo Program to convert arrays into list
Converted List Output1 = [Ram, Deepak, Divya, Google]
Converted List Output2 = [1000, 2000, 28376, 938478]

Method2 : Another way to convert arrays into list

ArraysToListConversionDemo.java

package com.mathapachi;

import java.util.ArrayList;
import java.util.List;

public class ArraysToListConversionDemo {

    public static void main(String[] args) {
       List list = new ArrayList();
       list.add("Ram");
       list.add(1000);
       list.add("Github");
       list.add("Google");
		
       System.out.println("List = "+list); 
    }

}

Output of the above program is as below:

List = [Ram, 1000, Github, Google]

Here, in method2 you can see the type of input for the array is String and Integer. If you want to restrict that only String should be added in the List then you can make it generic.

How to enable pretty print JSON output Using Gson

Gson can be used to enable pretty print JSON.

What is pretty print JSON?

Pretty print json is more formatted one. e.g. the below json is pretty print json. It is formatted and is easier to read and comprehend.

{
  "name": "DK",
  "company": "Google",
  "salary": 20000,
  "map": {
    "K3": "V3",
    "K1": "V1",
    "K2": "V2"
  }
}

The below example is without pretty print:

{"name":"DK","company":"Google","salary":20000,"map":{"K3":"V3","K1":"V1","K2":"V2"}}

The pretty print can be enabled for Json like this:

Gson gson = new GsonBuilder().setPrettyPrinting().create();

Without pretty print json, we can simply instantiate like this

Gson gson = new Gson();

Demo Example to enable pretty print JSON

Employee.java

package com.mathapachi.beans;

import java.util.Map;

public class Employee {

	private String name;
	private String company;
	private Integer salary;
	private Map map;
	
	public Employee(String name, String company, Integer salary,Map map) {
		this.name = name;
		this.company = company;
		this.salary = salary;
		this.map = map;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public Integer getSalary() {
		return salary;
	}
	public void setSalary(Integer salary) {
		this.salary = salary;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}
	
	
}

JsonPrettyPrintingDemo.java

package com.mathapachi.json;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mathapachi.beans.Employee;

public class JsonPrettyPrintingDemo {

	public static void main(String[] args) {
       Map map = new HashMap();
       map.put("K1", "V1");
       map.put("K2", "V2");
       map.put("K3", "V3");
	   Employee emp = new Employee("DK","Google",20000,map); 
       Gson gson = new GsonBuilder().setPrettyPrinting().create();
       System.out.println(gson.toJson(emp)); 
	}

}

The output of the above demo program is as follow:

{
  "name": "DK",
  "company": "Google",
  "salary": 20000,
  "map": {
    "K3": "V3",
    "K1": "V1",
    "K2": "V2"
  }
}