Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * join the string tuple with the separator char sequence.
     * if the value of map is null ,just store the key.
     * @param map
     *           the key,value tuple.
     * @param separator
     *          the join string
     * @return
     *         the joined string list.
     */
    public static List<String> join(Map<String, String> map, String separator) {
        if (map == null) {
            return null;
        }
        List<String> result = new ArrayList<String>();
        if (map == null || map.size() == 0) {
            return result;
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (value == null || value.length() == 0) {
                result.add(key);
            } else {
                result.add(key + separator + value);
            }
        }
        return result;
    }

    /**
     * join the string list with the join char sequence.
     * 
     * @param list
     *           the join sequence.
     * @param separator
     *           the join key word;
     * @return
     *          joined string.
     */
    public static String join(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (String ele : list) {
            if (sb.length() > 0) {
                sb.append(separator);
            }
            sb.append(ele);
        }
        return sb.toString();
    }
}