Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

public class Main {

    public final static <T> String join(List<T> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).toString()).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    public final static <T> String join(Queue<T> queue, String separator) {
        StringBuilder sb = new StringBuilder();
        for (T t : queue) {
            sb.append(t.toString()).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - separator.length());
    }

    public final static <T> String join(Set<T> set, String separator) {
        StringBuilder sb = new StringBuilder();
        for (T t : set) {
            sb.append(t.toString()).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - separator.length());
    }

    public final static <K, V> String join(Map<K, V> map, String separator, String separator1) {
        if (map == null || map.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<K, V> entry : map.entrySet()) {
            sb.append(String.valueOf(entry.getKey())).append(separator1).append(String.valueOf(entry.getValue()))
                    .append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - separator.length());
    }
}