Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Created April 1, 2015 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevin-lee/8eb27d4282f19db404bd to your computer and use it in GitHub Desktop.
Save kevin-lee/8eb27d4282f19db404bd to your computer and use it in GitHub Desktop.
Old Java vs Java 8 - read file and keep unique words
aaa ccc
ddd
bbb
aaa bbb ccc ddd
bbb ddd
aaa bbb
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
/**
* @author Lee, SeongHyun (Kevin)
* @since 2015-04-01
*/
public class Java8UniqueWords {
public static void main(String[] args) throws IOException {
System.out.print("Enter filename: ");
try (final Stream<String> lines = Files.lines(Paths.get(new Scanner(System.in).nextLine()))
.map(line -> line.split("[\\s]+"))
.flatMap(Arrays::stream)
.distinct()
.sorted()) {
final String uniqueWords = lines.collect(joining(", "));
System.out.println(uniqueWords);
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* @author Lee, SeongHyun (Kevin)
* @since 2015-04-01
*/
public class OldJavaUniqueWords {
public static void main(String[] args) throws IOException {
System.out.print("Enter filename: ");
String filename = new Scanner(System.in).nextLine();
List<String> uniqueWords = new ArrayList<>();
try (FileReader fileReader = new FileReader(new File(filename));
BufferedReader reader = new BufferedReader(fileReader)) {
String line = reader.readLine();
while (line != null) {
String[] words = line.split("[\\s]+");
for (String word : words) {
if (!uniqueWords.contains(word)) {
uniqueWords.add(word);
}
}
line = reader.readLine();
}
}
Collections.sort(uniqueWords);
System.out.println(uniqueWords);
}
}
@kevin-lee
Copy link
Author

Old Java, using crappy low-level external loop

# After compilation,
java OldJavaUniqueWords 

Enter filename: data.txt
[aaa, bbb, ccc, ddd]

Java 8, a declarative way using Stream (Monad)

# After compilation,
java Java8UniqueWords 

Enter filename: data.txt
aaa, bbb, ccc, ddd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment