//EmptyStringEliminationWithDashJoin.java

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class EmptyStringEliminationWithDashJoin
{
    public static void main(String args[])
    {
        //We will combine non-empty strings from the following list of
        //strings into a single string with the parts separated by a dash:
        List<String> strings
            = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl", "mno");
        System.out.println("\nList of strings: " + strings);

        System.out.println("\n=============");
        System.out.println("Using Java 7: ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : strings)
        {
            if (!string.isEmpty())
            {
                stringBuilder.append(string);
                stringBuilder.append("-");
            }
        }
        String mergedString = stringBuilder.toString();
        System.out.println
        (
            "Single string formed by combining all "
            + "non-empty\nstrings, separated by a single dash:\n"
            + mergedString.substring(0, mergedString.length() - 1)
        );

        System.out.println("\n=============");
        System.out.println("Using Java 8: ");
        System.out.println
        (
            "Single string formed by combining all "
            + "non-empty\nstrings, separated by a single dash:\n"
            + strings
            .stream()
            .filter(string -> !string.isEmpty())
            .collect(Collectors.joining("-"))
        );
    }
}
/*  Output:

    List of strings: [abc, , bc, efg, abcd, , jkl, mno]

    =============
    Using Java 7:
    Single string formed by combining all non-empty
    strings, separated by a single dash:
    abc-bc-efg-abcd-jkl-mno

    =============
    Using Java 8:
    Single string formed by combining all non-empty
    strings, separated by a single dash:
    abc-bc-efg-abcd-jkl-mno
*/
