-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearch Engine.Java
More file actions
31 lines (25 loc) · 817 Bytes
/
Search Engine.Java
File metadata and controls
31 lines (25 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchEngine {
private Map<String, List<String>> searchIndex;
public SearchEngine() {
searchIndex = new HashMap<>();
}
public void indexDocument(String document, List<String> keywords) {
for (String keyword : keywords) {
if (searchIndex.containsKey(keyword)) {
searchIndex.get(keyword).add(document);
} else {
searchIndex.put(keyword, new ArrayList<>(List.of(document)));
}
}
}
public List<String> search(String query) {
List<String> results = new ArrayList<>();
if (searchIndex.containsKey(query)) {
results.addAll(searchIndex.get(query));
}
return results;
}
}