Several Useful Items
Collectors
This static service
class provides terminal operations for streams. Here are a few
handy methods.
Collectors.toCollection(ArrayList::new)
will dump your stream into an array list.Collectors.toCollection(TreeSet::new)
will dump your stream into an a TreeSet, which maintains sortable items in order.Collectors.toList()
will dump your stream into an a List.- String has
join
. Can we "join" items in a stream?String joined = source.map(e -> e.toString()) .joining("|");
Flat as a Pancake
You might wonder about these.
- Can I get a stream of words out of a file?
- Can I get a character stream from a file?
You don't get a character stream from a file, you get
an IntStream
; characters are known by their
ASCII value.
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.IntStream;
public class ByChars
{
public static void main(String[] args)
{
String fileName = args[0];
try
(
BufferedReader br = Files.newBufferedReader(
Path.of(fileName));
)
{
IntStream is = br.lines()
.map(e -> e + "\n")
.flatMapToInt( e -> e.chars());
is.forEach( e -> System.out.print((char) e));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
Questions
- What do you think
flatMapToInt
is doing? Do yoiu have a reasonble explanation?
- Dumb question: How can I modify this program to get the sum of the ASCII values of all of the characters in the stream?
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Stream;
import java.util.Arrays;
import java.nio.file.Files;
import java.nio.file.Path;
public class ByWords
{
public static void main(String[] args)
{
try
{
BufferedReader br = Files.newBufferedReader(
Path.of(args[0]));
Stream<String> is = br.lines()
.flatMap( e -> Arrays.stream(e.split("\\s+")));
is.forEach(System.out::println);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
- What do you think
flatMap
is doing? Do yoiu have a reasonble explanation?
- Dumb question: How can I modify this program to count the number of words in the stream? How about all words containing four or more letters?
Some Stream Exercises
Download this file wafm.txt. It will be used to test the capabilities of your program.
public class StreamPuzzles
{
public static void main(String[] args)
{
}
/**
* this uses stream operations to filter all
* duplicates of of a wordlist file and to place it in a
* List<String>
*/
public static ArrayList<String> noDuplicates(String fileName)
{
}
//this verson does not use streams
public static ArrayList<String> noDuplicates1(String fileName)
{
}
/**
* find the first n words in a file beginning with
* the character ch and place them in a
* list of strings.
*/
public static List<String> findFirstN(String fileName, char ch, int n)
{
}
//no streams version
public static List<String> findFirstN(String fileName, char ch, int n)
{
}
/**
* removes all nonwhitespace, nonalphanumeric characters
* from the contents of a file and places it in a filen
* named fileName.out.
*/
public static vood depunct(String fileName)
{
}
/**
* Extracts all distinct words used in a file and places
* them in a file named fileName.voc. Can you extract
* the vocabulary from War and Peace (file provided here)?
*/
public static void vocabulary(String fileName)
{
}
}