programming-examples/java/Data_Structures/Whitelist.java

68 lines
2.0 KiB
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
import edu.princeton.cs.introcs.In;
import edu.princeton.cs.introcs.StdIn;
import edu.princeton.cs.introcs.StdOut;
/*************************************************************************
* Compilation: javac Whitelist.java
* Execution: java Whitelist whitelist.txt < data.txt
* Dependencies: StaticSetOfInts.java In.java StdOut.java
*
* Data files: http://algs4.cs.princeton.edu/11model/tinyW.txt
* http://algs4.cs.princeton.edu/11model/tinyT.txt
* http://algs4.cs.princeton.edu/11model/largeW.txt
* http://algs4.cs.princeton.edu/11model/largeT.txt
*
* Whitelist filter.
*
*
* % java Whitelist tinyW.txt < tinyT.txt
* 50
* 99
* 13
*
* % java Whitelist largeW.txt < largeT.txt | more
* 499569
* 984875
* 295754
* 207807
* 140925
* 161828
* [367,966 total values]
*
*************************************************************************/
/**
* The Whitelist class provides a client for reading in
* a set of integers from a file; reading in a sequence of integers
* from standard input; and printing to standard output those
* integers not in the whitelist.
*
* For additional documentation, see Section 1.2</a> of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Whitelist {
/**
* Reads in a sequence of integers from the whitelist file, specified as
* a command-line argument. Reads in integers from standard input and
* prints to standard output those integers that are not in the file.
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] white = in.readAllInts();
StaticSETofInts set = new StaticSETofInts(white);
// Read key, print if not in whitelist.
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (!set.contains(key))
StdOut.println(key);
}
}
}