1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.nio.charset.StandardCharsets;
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.stream.Stream;
|
---|
12 |
|
---|
13 |
|
---|
14 | public class TestWhitelistHashmap {
|
---|
15 |
|
---|
16 | protected String _dictionary_filename;
|
---|
17 | protected HashMap<String,Boolean> _hashmap;
|
---|
18 |
|
---|
19 | protected int FILL_FACTOR = 26;
|
---|
20 |
|
---|
21 | public TestWhitelistHashmap(String dictionary_filename) {
|
---|
22 | System.out.println("Constructing: WhitelistHashmap");
|
---|
23 |
|
---|
24 | _dictionary_filename = dictionary_filename;
|
---|
25 |
|
---|
26 | _hashmap = new HashMap<String,Boolean>();
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void storeEntries()
|
---|
30 | {
|
---|
31 | System.out.println("Build hashmap ...");
|
---|
32 |
|
---|
33 | for (int i=0; i<FILL_FACTOR; i++) {
|
---|
34 | char prefix = (char) ('a' + i);
|
---|
35 |
|
---|
36 | //read file into stream, try-with-resources
|
---|
37 | try (Stream<String> stream = Files.lines(Paths.get(_dictionary_filename))) {
|
---|
38 |
|
---|
39 | stream.forEach(word -> {_hashmap.put(prefix+word,true);});
|
---|
40 | } catch (IOException e) {
|
---|
41 | e.printStackTrace();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | System.out.println("... done");
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | public boolean contains(String key)
|
---|
49 | {
|
---|
50 | return _hashmap.containsKey(key);
|
---|
51 | }
|
---|
52 |
|
---|
53 | }
|
---|