Created
April 1, 2015 12:56
-
-
Save kevin-lee/8eb27d4282f19db404bd to your computer and use it in GitHub Desktop.
Old Java vs Java 8 - read file and keep unique words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aaa ccc | |
ddd | |
bbb | |
aaa bbb ccc ddd | |
bbb ddd | |
aaa bbb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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