/home/wpollock1/public_html/AJava/DestutterStream.java
import java.util.*;
import java.util.stream.Collectors;
/** Destuddering a list means to remove adjacent duplicates;
* for example, [1, 4, 2, 2, 3, 3, 5, 4] ==> [1, 4, 2, 3, 5, 4].
* (Uses include removing extra spaces between words in a document,
* removing duplicates from a sorted list (producing a "set"), and others)
* In this case, the list is the command-line arguments (Strings).
* For fun, try this: java DestutterStream "" foo
*
* In this version, we destudder a list using the new Java 8 Streams.
*
* @author Wayne Pollock, Tampa Florida USA
*/
public class DestutterStream {
public static void main ( String [] args ) {
// Local variables can be used in a Lambda (or local class) only
// if they are final. Thus, "previous" is a final variable to an
// array of one string (and arrays are mutable). Tricky!
final String[] previous = { null };
String result = Arrays.stream( args )
.filter( arg -> { boolean isDifferent = ! arg.equals(previous[0]);
previous[0] = arg;
return isDifferent;
})
.collect( Collectors.joining(", ") );
System.out.println( result );
}
}