import java.io.Reader;
import java.io.FileReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.charset.*;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.concurrent.*;
class MyCounter implements Runnable {
private ConcurrentMap<String, Integer> map = null;
private String line = null;
public MyCounter(String line, ConcurrentMap<String, Integer> map) {
this.line = line;
this.map = map;
}
@Override
public void run() {
for (String word : line.split(" ")) {
// System.out.println(word);
increase(word);
}
}
public int increase(String word) {
Integer oldValue;
Integer newValue;
while (true) {
oldValue = map.get(word);
if (oldValue == null) {
newValue = 1;
if (map.putIfAbsent(word, newValue) == null)
break;
} else {
newValue = oldValue + 1;
if (map.replace(word, oldValue, newValue))
break;
}
}
return newValue;
}
}
public class ConcurrentTest {
private static final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
public static void main(String[] args) throws IOException {
Path p = Paths.get("F:\\hamlet.txt");
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
ExecutorService service = Executors.newFixedThreadPool(10);
for (String line : lines) {
// new Thread(new MyCounter(line,map)).start();
line = line.toLowerCase();
// Split str around matches of empty string ""
String[] substrings = "!'\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~".split("");
for (String ch : substrings) {
line = line.replace(ch, " ");
}
service.execute(new MyCounter(line, map));
}
service.shutdown();
while (!service.isTerminated())
;
// Iterator i = map.keySet().iterator();
//
// while (i.hasNext()) {
// String word = (String) i.next();
// int count = map.get(word);
// System.out.println(word + " " + count);
// }
List<String> values = new ArrayList<>(map.keySet());
Collections.sort(values, new Comparator<String>() {
public int compare(String a, String b) {
// no need to worry about nulls as we know a and b are both in pl
return map.get(b) - map.get(a);
}
});
for (String val : values) {
System.out.println(val + "," + map.get(val));
}
}
}
,4138
the,1143
and,966
to,762
of,669
i,631
you,554
a,546
my,514
hamlet,471
in,451