Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Tuesday, October 2, 2018
jsqlparser 에서 Unicode 사용하기
jsqlparser 에서 Unicode 사용하기: jsqlparser 의 option 이 Unicode 입력이 불가하게 설정되어서 한글 입출력이 안 됨.
Friday, September 28, 2018
Java - counting specific words
https://codereview.stackexchange.com/questions/73486/counting-five-specific-words-in-a-file
6 down vote accepted
6 down vote accepted
Here's a better, more efficient and compact way:
String path = "C:/Users/n/Desktop/Text.txt";
List<String> targetList = Arrays.asList("a", "the", "bird", "animal", "is");
Map<String, Integer> counts = new HashMap<>(targetList.size());
for (String word : targetList) {
counts.put(word, 0);
}
for (String line : Files.readAllLines(Paths.get(path))) {
for (String word : line.replaceAll("[!?.,]", "").toLowerCase().split("\\s+")) {
Integer count = counts.get(word);
if (count != null) {
counts.put(word, count + 1);
}
}
}
System.out.print(counts.get(targetList.get(0)));
for (int i = 1; i < targetList.size(); ++i) {
String word = targetList.get(i);
System.out.print(" " + counts.get(word));
}
System.out.println();
The improvements and corrections:- It's good to define constants like the path high up in a file where they are easy to change and easy to change, without having to read into the details of the code
- It's simpler to write paths with forward slashes
- Use interface type like
List
when defining a list instead of implementation type likeArrayList
- Since it seems you're only interested in a specific set of words:
- I put them in a list for ordering
- ... then initialized the map of counts to all 0 values
- Instead of building a list of words, it's more efficient to do the counting at the same time as you read the words. This will save you both storage and processing time
- When you do
line.replaceAll("[!?.,]", "")
, the operation is not performed online
, as strings in Java are immutable. The result with the characters removed is returned - The same goes for a
line.split("\\s+")
statement you had. If you don't save the result of the operation, then it's completely pointless
Subscribe to:
Posts (Atom)
-
https://chrome.google.com/webstore/detail/video-cc-translator/fhbpmacbgklobobcieiaoibpjhdnmcfn youtube, udemy, udacity 자막 번역기 크롬브라우저 확장...
-
ALT + CTRL + T 를 입력하여 터미널을 띄운다. 터미널에서 ccsm 실행 $ccsm 실행 없을 경우 설치 $apt-get install compoziconfig-settings-manager 설치후 컴피즈 설정 관리자(cc...
-
우분투 gui 환경 지우기 / 우분투 gui 없애기 1. 우분투 데스크탑 환경 제거 sudo apt-get remove ubuntu-desktop 2. GNOME 데스크톱 매니저 제거 sudo apt-get remove gdm3 3. 그래픽...