Archives: Array

Java – Bubble Sort Array Example

Java – Convert Array to Map using Commons ArrayUtils

This example use the Apache Commons lang’s ArrayUtils class to convert a two dimensional array to a Map object.

[code lang=”java”]import java.util.Map;
import org.apache.commons.lang.ArrayUtils;

public class ArrayToMapExample {

public static void main(String[] args) {
// A two dimensional array of countries capital.
String[][] countries = {{"United States", "New York"},
{"United Kingdom", "London"},
{"Netherlands", "Amsterdam"},
{"Japan", "Tokyo"},
{"France", "Paris"}};

// To convert an array to a Map each array elements must
// be an array with at least two elements where the first
// element will be the key and the second element will be
// the value.
Map countryCapitals = ArrayUtils.toMap(countries);

System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));
System.out.println("Capital of France is " + countryCapitals.get("France"));


Java – Convert Array to List

[code lang=”java”]import java.util.*;

public class ArrayToCollection {

public static void main(String args[]) throws IOException {

BufferedReader in = new BufferedReader
(new InputStreamReader(;
System.out.println("How many elements you want to add to the array: ");
int n = Integer.parseInt(in.readLine());
String[] name = new String[n];
for(int i = 0; i < n; i++) {
name[i] = in.readLine();
List list = Arrays.asList(name);
for(String li: list) {
String str = li;
System.out.print(str + " ");



How many elements you want to add to the array:
red white green

red white green