For future reference, here is a solution with Google Guava.
Some setup code to create the list from the question's string:
String stringsString = "String1-1, String5, String6, String1-2, String5, String6, String1-3, String5, String6";
String[] stringsArray = stringsString.split(", ");
Iterable<String> stringsList = Arrays.asList(stringsArray);
And now filter out the unneeded items:
Iterable<String> filtered = Iterables.filter(stringsList, new Predicate<String>() {
@Override
public boolean apply(String input) {
return !input.startsWith("String1-");
}
});
This simple routine creates a new Iterable
that contains only the elements that you specified. The beauty of this is that it is executed lazily (evaluated only when you really want to use it) and it does not modify the original collection. Making your software state immutable (unmodifiable) is in most cases the best you can do. Handling mutable collections is hard and they are often the cause of software errors.