Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collections;

import java.util.List;

public class Main {
    public static String sortAndFlattenNameList(List<String> names) {
        Collections.sort(names);

        StringBuilder sb = new StringBuilder();

        for (String name : names) {
            sb.append(name + ", ");
        }

        String flattenedNames = sb.toString();

        if (flattenedNames.length() > 2) {
            // drop the last comma and space
            flattenedNames = flattenedNames.substring(0, flattenedNames.length() - 2);
        }

        return flattenedNames;
    }
}