如何减少 HashMap 搜索的运行时间?
How to reduce runtime of HashMap searching?
我被要求制作这个程序。下面是示例程序流程。
// first input: how many data you want to input. Data is consisted of name and phone number. Both are string.
3
// here is the data-entry procedure. I use HashMap. 3 iteration to input the names and phone numbers
hans
12345678
dion
123456789
harry
12345671
// after the data is entered, I entered (also 3, taken from the entry at first input) 3 names to be searched within the HashMap. This can be any name that you can think of. As long as the name are in the HashMap, the program will display the person's name and his/her phone num.
herry
hans
harry
//result will be written as 'not found' if the key (name) is not present in HashMap. This is the sample output
not found
hans=12345679
harry=12345671
这是程序的限制。
其实我的程序已经运行成功了。这是我正在使用的代码,输出的屏幕截图附在下面。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class HM2a {
static List<String> listQuery = new ArrayList<String>();
public static void loop(String name, HashMap<String, String> phonebook) {
String key, value;
String result = "";
for (Map.Entry<String, String> entry : phonebook.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (name.equals(key)) {
result = key + "=" + value;
listQuery.add(result);
return;
}
}
result = "Not found";
listQuery.add(result);
}
public static void main(String[] args) throws IOException {
HashMap<String, String> phonebook = new HashMap<String, String>();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
int entries = Integer.parseInt(bufferedReader.readLine());
String query = "";
String list = "";
for (int i = 0; i < entries; i++) {
String name = bufferedReader.readLine();
String phonenum = bufferedReader.readLine();
name = name.toLowerCase();
phonebook.put(name, phonenum);
}
for (int i = 0; i < entries; i++) {
query = bufferedReader.readLine();
query = query.toLowerCase();
loop(query, phonebook);
}
for (int i = 0; i < listQuery.size(); i++) {
System.out.println(listQuery.get(i));
}
} catch (Exception e) {
System.out.println(e);
}
}
}
NetBeans 的 运行 输出:
问题是,我的代码是由我校园使用的本地自动评分器(与 HackerRank 类似)评分的,它一直说(从测试用例中)我的 运行时间总是超过 5 秒3例。不幸的是,我无法询问测试用例是什么。
有什么方法可以让我的代码更高效,尤其是搜索算法,以减少 运行 时间?老实说,我 运行 没主意了。一开始我用的是scanner,后来发现scanner占用内存大,改成了BufferedReader。它出现了问题,但现在我面临 运行时间问题。
这是自动评分器显示的结果。
根据上面评论的建议,我使用 get
修改了这部分搜索。这就是修改。我也删除了 loop
方法。
for (int i = 0; i < entries; i++) {
query = bufferedReader.readLine();
query = query.toLowerCase();
if (phonebook.get(query) == null) {
String result = "Not found";
listQuery.add(result);
} else {
String result = query + "=" + phonebook.get(query);
listQuery.add(result);
}
}
这是最终结果。已被评为满分
我被要求制作这个程序。下面是示例程序流程。
// first input: how many data you want to input. Data is consisted of name and phone number. Both are string.
3
// here is the data-entry procedure. I use HashMap. 3 iteration to input the names and phone numbers
hans
12345678
dion
123456789
harry
12345671
// after the data is entered, I entered (also 3, taken from the entry at first input) 3 names to be searched within the HashMap. This can be any name that you can think of. As long as the name are in the HashMap, the program will display the person's name and his/her phone num.
herry
hans
harry
//result will be written as 'not found' if the key (name) is not present in HashMap. This is the sample output
not found
hans=12345679
harry=12345671
这是程序的限制。
其实我的程序已经运行成功了。这是我正在使用的代码,输出的屏幕截图附在下面。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class HM2a {
static List<String> listQuery = new ArrayList<String>();
public static void loop(String name, HashMap<String, String> phonebook) {
String key, value;
String result = "";
for (Map.Entry<String, String> entry : phonebook.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (name.equals(key)) {
result = key + "=" + value;
listQuery.add(result);
return;
}
}
result = "Not found";
listQuery.add(result);
}
public static void main(String[] args) throws IOException {
HashMap<String, String> phonebook = new HashMap<String, String>();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
int entries = Integer.parseInt(bufferedReader.readLine());
String query = "";
String list = "";
for (int i = 0; i < entries; i++) {
String name = bufferedReader.readLine();
String phonenum = bufferedReader.readLine();
name = name.toLowerCase();
phonebook.put(name, phonenum);
}
for (int i = 0; i < entries; i++) {
query = bufferedReader.readLine();
query = query.toLowerCase();
loop(query, phonebook);
}
for (int i = 0; i < listQuery.size(); i++) {
System.out.println(listQuery.get(i));
}
} catch (Exception e) {
System.out.println(e);
}
}
}
NetBeans 的 运行 输出:
问题是,我的代码是由我校园使用的本地自动评分器(与 HackerRank 类似)评分的,它一直说(从测试用例中)我的 运行时间总是超过 5 秒3例。不幸的是,我无法询问测试用例是什么。
有什么方法可以让我的代码更高效,尤其是搜索算法,以减少 运行 时间?老实说,我 运行 没主意了。一开始我用的是scanner,后来发现scanner占用内存大,改成了BufferedReader。它出现了问题,但现在我面临 运行时间问题。
这是自动评分器显示的结果。
根据上面评论的建议,我使用 get
修改了这部分搜索。这就是修改。我也删除了 loop
方法。
for (int i = 0; i < entries; i++) {
query = bufferedReader.readLine();
query = query.toLowerCase();
if (phonebook.get(query) == null) {
String result = "Not found";
listQuery.add(result);
} else {
String result = query + "=" + phonebook.get(query);
listQuery.add(result);
}
}
这是最终结果。已被评为满分