Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Takes a collection and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using a {@link List}
     * separated by "\n":
     * <br>
     * <code>
     * List list = new ArrayList();
     * <br>
     * list.add(1);
     * <br>
     * list.add(2);
     * <br>
     * list.add(3);
     * <br>
     * String output = CollectionUtils.join(list, "\n");
     * <br>
     * </code>
     * 
     * @param collection a {@link Collection} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(Collection<?> collection, String separator) {
        String output = "";
        if (collection != null) {
            Iterator<?> iterator = collection.iterator();
            while (iterator.hasNext()) {
                Object o = iterator.next();
                output += o;
                if (iterator.hasNext())
                    output += separator;
            }
        }
        return output;
    }

    /**
     * Takes an array of Objects and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link Integer[]}
     * separated by "\n":
     * <br>
     * <code>
     * Integer[] array = {1, 2, 3};
     * <br>
     * String output = CollectionUtils.join(array, "\n");
     * <br>
     * </code>
     * 
     * @param collection a {@link Collection} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(Object[] collection, String separator) {
        String output = "";
        if (collection != null) {
            for (int i = 0; i < collection.length - 1; i++) {
                Object o = collection[i];
                output += o;
                output += separator;
            }
            if (collection.length > 0)
                output += collection[collection.length - 1];
        }
        return output;
    }

    /**
     * Takes an array of booleans and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link boolean[]}
     * separated by ", ":
     * <br>
     * <code>
     * boolean[] array = {true, true, false};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link boolean[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(boolean[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Boolean.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Boolean.toString(collection[collection.length - 1]);
        return output;
    }

    /**
     * Takes an array of chars and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link char[]}
     * separated by ", ":
     * <br>
     * <code>
     * boolean[] array = {true, true, false};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link boolean[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(char[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Character.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Character.toString(collection[collection.length - 1]);
        return output;
    }

    /**
     * Takes an array of doubles and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link double[]}
     * separated by ", ":
     * <br>
     * <code>
     * double[] array = {1.0, 2.0, 3.0};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link double[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(double[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Double.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Double.toString(collection[collection.length - 1]);
        return output;

    }

    /**
     * Takes an array of primitive integer and returns all elements assembled in
     * a {@link String} joined by the defined separator.
     * <br>
     * Example: Create a {@link String} using an {@link int[]}
     * separated by ", ":
     * <br>
     * <code>
     * int[] array = {1, 2, 3};
     * <br>
     * String output = CollectionUtils.join(array, ", ");
     * <br>
     * </code>
     * 
     * @param collection a {@link int[]} of objects to join.
     * @param separator the {@link String} separator used to join the collection. 
     * @return {@link String} joined string.
     */
    public static String join(int[] collection, String separator) {
        String output = "";
        for (int i = 0; i < collection.length - 1; i++) {
            String o = Integer.toString(collection[i]);
            output += o + separator;
        }
        if (collection.length > 0)
            output += Integer.toString(collection[collection.length - 1]);
        return output;
    }
}