Skip to content

Instantly share code, notes, and snippets.

@meierjan
Created November 28, 2013 00:28
Show Gist options
  • Save meierjan/7685431 to your computer and use it in GitHub Desktop.
Save meierjan/7685431 to your computer and use it in GitHub Desktop.
package de.hstrier.rockyou;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Reader {
private HashMap<String, Integer> hashmap;
public Reader(File fileToRead, HashMap<String, Integer> hashmap)
throws FileNotFoundException {
this.hashmap = hashmap;
this.readFile(fileToRead);
}
private void readFile(final File file) throws FileNotFoundException {
Runnable r = new Runnable() {
@Override
public void run() {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF8"))) {
String line = null;
String value;
while ((line = reader.readLine()) != null) {
value = line.trim();
Reader.this.incrementHashMapValue(value);
}
printHashMap();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Thread t = new Thread(r);
t.start();
}
public synchronized void incrementHashMapValue(String key) {
Integer value = this.hashmap.get(key);
if (value != null) {
this.hashmap.put(key, new Integer(value.intValue() + 1));
} else {
this.hashmap.put(key, 1);
}
}
public static void main(String[] args) throws FileNotFoundException {
File fileToRead = new File("/Users/janmeier/Downloads/rockyou.txt");
HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
Reader reader = new Reader(fileToRead, hashmap);
}
private void printHashMap() {
for (Map.Entry<String, Integer> elem : this.hashmap.entrySet()) {
if (elem.getValue() > 1) {
System.out
.println(elem.getKey() + " times: " + elem.getValue());
}
}
System.out.println(this.hashmap.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment