//EmptyStringElimination.java

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

public class EmptyStringElimination
{
    public static void main(String args[])
    {
        //We will eliminate empty strings from the following list of strings:
        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: ");
        List<String> filteredList = new ArrayList<String>();
        for (String string : strings)
        {
            if (!string.isEmpty())
            {
                filteredList.add(string);
            }
        }
        System.out.println
        (
            "List of strings with empty ones removed: "
            + filteredList
        );

        System.out.println("\n=============");
        System.out.println("Using Java 8: ");
        System.out.println
        (
            "List of strings with empty ones removed: "
            + strings
            .stream()
            .filter(string -> !string.isEmpty())
            .collect(Collectors.toList())
        );
    }
}
/*  Output:

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

    =============
    Using Java 7:
    List of strings with empty ones removed: [abc, bc, efg, abcd, jkl, mno]

    =============
    Using Java 8:
    List of strings with empty ones removed: [abc, bc, efg, abcd, jkl, mno]
*/
