Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- public class Indexer {
- private HashMap<String, ArrayList<String>> index;
- public Indexer() {
- index = new HashMap<String, ArrayList<String>>();
- }
- public void addEntry(String location, String[] words) {
- for (int i = 0; i < words.length; i++) {
- String w = words[i].toLowerCase();
- if (index.containsKey(w)) {
- ArrayList<String> locations = index.get(w);
- if (!locations.contains(location)) {
- locations.add(location);
- }
- }
- else {
- ArrayList<String> locations = new ArrayList<String>();
- locations.add(location);
- index.put(w, locations);
- }
- }
- }
- public String search(String word) {
- String searchTerm = word.toLowerCase();
- ArrayList<String> locations = index.get(searchTerm);
- String result = "";
- if (locations == null)
- return result;
- for (int i = 0; i < locations.size(); i++) {
- String location = locations.get(i);
- result = result + location + ", ";
- }
- result = result.substring(0, (result.length()-2));
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement