【发布时间】:2021-06-09 11:57:40
【问题描述】:
我的代码有问题,调用方法somme_2时运行程序耗时太长,我想减少运行时间。顺便说一句,我在这个程序中使用的 txt 文件包含近 500_000 行。你知道如何解决它吗?
这是我的主线
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Somme2 somme2 = new Somme2("src/dico.txt");
//somme2.remove(alphabeticalOrder("Amsterdam" + "ADN"), "adn");
//somme2.remove(alphabeticalOrder("Amsterdam" + "ADN"), "riflassent");
somme2.somme_2(alphabeticalOrder("volontiers" + "tranquillement"));
}
private static String alphabeticalOrder(String word) {
word = word.toLowerCase();
List<Character> list = new ArrayList<>();
for (int i = 0; i < word.length(); i++) {
list.add(word.charAt(i));
}
Collections.sort(list);
String string = "";
for (int i = 0; i < list.size(); i++) {
string = string + list.get(i);
}
return string;
}
}
这是我的类,其中包含 somme_2 函数:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Somme2 {
private String path;
public Somme2(String path) {
this.path = path;
}
/***
* values() returns a list which contains every word in our file.
* @return List<String>
* @throws FileNotFoundException
*/
private List<String> values() throws IOException {
return Files.readAllLines(Path.of(path));
}
/***
* alphabeticalOrder() returns the word in the parameter, alphabetically
* @param word
* @return String
*/
private String alphabeticalOrder(String word) {
word = word.toLowerCase();
char[] chars = word.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
/***
* addToHashMap() returns a hashMap, which uses as a key alphabetically words, and as value classical words
* @return HashMap<String, List<String>>
* @throws FileNotFoundException
*/
private HashMap<String, List<String>> addToHashMap() throws IOException {
HashMap<String, List<String>> hashMap = new HashMap<>();
for (String value : values()) {
String word = alphabeticalOrder(value);
if (hashMap.containsKey(word)) {
hashMap.get(word).add(value);
}
else {
List<String> list = new ArrayList<>();
list.add(value);
hashMap.put(word, list);
}
}
return hashMap;
}
/***
* findTheLetter return the index of the letter
* @param letter
* @param word
* @return
*/
private int findTheLetter(char letter, char[] word) {
for (int i = 0; i < word.length; i++) {
if (word[i] == letter) {
return i;
}
}
return 0;
}
private char[] removeLetter(char letter, char[] word) {
int x = findTheLetter(letter, word);
char[] newWord;
if (word.length == 1) {
newWord = new char[word.length];
} else {
newWord = new char[word.length - 1];
}
for (int i = 0; i < word.length - 1; i++) {
if (i < x) {
newWord[i] = word[i];
}
else {
newWord[i] = word[i + 1];
}
}
return newWord;
}
public String remove(String word, String string) {
char[] myWord = string.toCharArray();
char[] words = word.toCharArray();
for (int i = 0; i < string.length(); i++) {
words = removeLetter(myWord[i], words);
}
return new String(words);
}
/***
* somme_2
* @param alph
* @throws FileNotFoundException
*/
public void somme_2(String alph) throws IOException {
HashMap<String, List<String >> hashMap = addToHashMap();
List<String> strings = new ArrayList<>();
strings.addAll(hashMap.keySet());
String alphWord = "" + alph;
for (String string : strings) {
if (hashMap.containsKey(remove(alphabeticalOrder(alphWord), string))) {
System.out.println("\"" + hashMap.get(string) + "\" and \"" + hashMap.get(remove(alphWord, string)) + "\" give \"" + alphWord + "\"");
return;
}
}
System.out.println("There are no words");
}
}
如果你想知道,这是 txt 文件的一部分:
A
ABS
ADN
ADNc
ADP
ADSL
AIEA
ARN
ARNm
ASBL
ASC
ASCII
AUD
Aarhus
Aaron
Aarschot
Abbeville
Abd
Abdelkader
Abel
Abidjan
Abitibi-Témiscamingue
Abkhazie
Abraham
Abu
Abuja
Abymes
Abyssinie
Acadie
Acapulco
Accra
Achaïe
Achgabat
我只是解决了时间问题,但现在我的程序有时会显示如下结果: “[宪法化]”和“[V,v]”给出“aeeeiilllmnnnooqrrstttuv”
这是错误的,因为按字母顺序排列的“宪法”+“V”不给出“aeeeiilllmnnnooqrrstttuv”。
感谢您的帮助!
【问题讨论】:
-
somme_2的目的是什么? -
顺便说一句:
x.hashCode() == y.hashCode()并不意味着x == y或x.equals(y);它仅表示x maybe == y或maybe x.equals(y)。 -
你用的是什么java版本?
-
somme_2 的目的是在我的文件中找到 2 个按字母顺序排列的单词,与参数中我的单词相等,并打印 ""word1" 和 "word2" 给出 "parameter""跨度>
-
关于版本,我使用java 15 JDK
标签: java performance hashmap