1

I have an array list which holds a bunch of activities inputted by the user, each of them have a title which is inputted by the user. I want the title to be parsed and inputted into a hashmap. Now the user can perform a search, where they can type certain keywords. I need to use a hashmap to check which activity in the array list they are referring too.

The problem i am having is that i know we cant have more than one values assosiated with a particular key. For example if the title for activity one is : football game, i want football to = 1 and game to = 1 (which indicates its related to the first activity) so that when the user types in one of those words it will pull up this activity.

August
  • 12,410
  • 3
  • 35
  • 51
user4188409
  • 77
  • 2
  • 8
  • 2
    You didn't ask a question. – DaoWen Nov 07 '14 at 03:05
  • do you mean , one of your user input football, another input game, and you want the activity to be "football game", and then when any user uses "football" or "game", they can find this activity? What about there is a "basketball game", should the user get both of them when using "game" to search? – JaskeyLam Nov 07 '14 at 03:09
  • When inputing an activity initally, the user will give the title football game as the frist activity for example. Then for the second activity, he could input basketball game. Then when perforing a search he could type, game and both both football game and basketball game will show up. If the userinputs football game, only 1 will show up. – user4188409 Nov 07 '14 at 03:21
  • @user4188409, please check my answer and output and see if this is what you want. – JaskeyLam Nov 09 '14 at 09:04

5 Answers5

0

You could potentially use a hash map on a list of strings. Make your key be a unique integer and then store any words associated with that integer in the string list. That way "game" could be associated with "soccer game" or "hockey game". But it depends how you associate your strings with your keys I guess.

Scott
  • 465
  • 3
  • 10
  • 22
  • Why even use a `HashMap` if the keys are consecutive integers? You should just use an `ArrayList` in that case. – DaoWen Nov 07 '14 at 03:06
  • So if i have for example activities: 1) Soccer game 2)Hockey game 3)Football game. And I use .get(game) first, the three options would be the 3 above right? But then can I use .get(soccer) this time to narrow the search? How would this be implemented? – user4188409 Nov 07 '14 at 03:11
0

Map<From, To> looks like it can only map to single things. That is luckily not true since To can also be a List<To> or a Set<To> or even a Map again.

Using complex types as values has some downsides though. You have to create those sets/lists per entry and handle null values.

roughly like

private Map<String, List<Integer>> activityMap = new HashMap<>();
private void add(String key, Integer value) {
    List<Integer> list = activityMap.get(key);
    if (list == null) {
        // must create and add the list
        list = new ArrayList<>();
        activityMap.put(key, list);
    }
    list.add(value);
}

private List<Integer> getAll(String key) {
    List<Integer> list = activityMap.get(key);
    // simpler to use if there is never null as result.
    if (list == null)
        list = new ArrayList<>();
    return list;
}

private void remove(String key, Integer value) {
    List<Integer> list = activityMap.get(key);
    if (list == null)
        return;

    // here should probably be list.remove(Object) - it looks confusing with Integer though
    for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext();) {
        Integer listValue = iterator.next();
        if (listValue.equals(value)) {
            iterator.remove();
        }
    }
}

Guava Multimap is an implementation of such a structure in case libraries are an option.

zapl
  • 63,179
  • 10
  • 123
  • 154
0

Use a list as the value of the HashMap.

Map<String, List<String>> map = new HashMap<String, List<String>>();

Pseudo code:

Parse your title and find the keywords. 

If the keyword is a new one Then 
  add it to the HashMap as a key, and add the Activity_id as the first element of the list.

Else (keyword is already in the HashMap) Then 
  add Activity_id as the next element of the corresponding list.

When you search a keyword you can return the list with all the Activity_ids that matches the keyword

Example:

Input: 1 - soccer game | 2 - badminton game

This is what HashMap will look like

~KEY~     | ~VALUES(List)~
soccer    | 1
badminton | 2
game      | 1,2
Niroshan
  • 2,064
  • 6
  • 35
  • 60
0

I am not sure what is really want to put into hashmap, and I just assume it looks like this:

"user1" => "bascketbal = 1, footdball = 2"
"user2" => "football = 3"
"user3" => "pingpong = 1"

If so, you can use Map<String, Map<String, Integer>>, e.g:

Map userActiveData = new HashMap<String, Map<String, Integer>>();

//For each user, maybe in a loop
Map<String, Integer> active = new HashMap<String, Integer>();
active.put("football", 1);
active.put("game", 2);
userActiveData.put("user1", active);
Frank
  • 624
  • 9
  • 27
0

I think this is what you want, it supports the whole key and a only part of the activity to search, please check the output :

public class ActivityManager {

    Map<String, Set<String>> map = new HashMap<String, Set<String>>();

    public static void main(String[] args) {
        ActivityManager testMap = new ActivityManager();
        testMap.addActivity("football game");
        testMap.addActivity("basketball game"); 


        Set<String> football=testMap.getActivities("football");
        Set<String> basketball=testMap.getActivities("basketball");
        Set<String> game=testMap.getActivities("game");
        Set<String> footballGame=testMap.getActivities("football game");

        System.out.println("-------football------");
        printActivities(football);
        System.out.println("-------game------");
        printActivities(game);
        System.out.println("-------basketball------");
        printActivities(basketball);
        System.out.println("-------football game------");
        printActivities(footballGame);
    }




    public void addActivity(String activity) {
        String[] keyWords = activity.split(" ");// split key words, change to  what you want if needed
        Set<String> activities=null;
        for (String key : keyWords) {
            activities = map.get(key);
            if (activities == null) {// do not have any activities mapped to this key yet
                activities = new HashSet<String>();
                map.put(key, activities);
            }
            activities.add(activity);// put new value into it.  
        }
        if (keyWords.length>1){
            Set<String> activitiesUsingWholeKey =map.get(activity);//get the activities using the whole word
            if(activitiesUsingWholeKey==null){
                activitiesUsingWholeKey=new HashSet<String>();
                map.put(activity, activitiesUsingWholeKey);
            }
            activitiesUsingWholeKey.add(activity);
        }

    }

    public Set<String> getActivities(String key){
        return this.map.get(key);
    }

    private static void printActivities(Set<String> activities){
        for(String activity:activities)
            System.out.println(activity);
    }
}

Output :

-------football------
football game
-------game------
basketball game
football game
-------basketball------
basketball game
-------football game------
football game
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149