如何从字符串输入实现HashMap

时间:2014-11-07 02:56:51

标签: java string arraylist collections hashmap

我有一个数组列表,它包含用户输入的一系列活动,每个活动都有一个由用户输入的标题。我希望将标题解析并输入到hashmap中。现在,用户可以执行搜索,他们可以在其中键入特定关键字。我需要使用hashmap来检查它们引用的数组列表中的哪个活动。

我遇到的问题是我知道我们不能有一个以上与特定键相关的值。例如,如果活动一的标题是:足球比赛,我想要足球= 1并且比赛要= 1(这表明它与第一个活动相关),这样当用户输入其中一个单词时,它将拉出这个活动。

5 个答案:

答案 0 :(得分:0)

您可以在字符串列表中使用哈希映射。使您的密钥成为唯一的整数,然后将与该整数关联的任何单词存储在字符串列表中。这样"游戏"可以与足球比赛"或者"曲棍球比赛"。但这取决于你将字符串与键相关联的方式。

答案 1 :(得分:0)

Map<From, To>看起来只能映射到单个内容。幸运的是,这不是真的,因为To也可以是List<To>Set<To>,甚至是Map

使用复杂类型作为值有一些缺点。您必须为每个条目创建这些集/列表并处理空值。

大致像

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是这种结构的实现,以防库是一种选择。

答案 2 :(得分:0)

使用list作为HashMap的值。

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

伪代码:

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.

当您搜索关键字时,您可以返回包含与关键字匹配的所有Activity_id的列表

示例:

输入:1 - soccer game | 2 - badminton game

这就是HashMap的样子

~KEY~     | ~VALUES(List)~
soccer    | 1
badminton | 2
game      | 1,2

答案 3 :(得分:0)

我不确定什么是真正想要放入hashmap,我只是假设它看起来像这样:

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

如果是这样,您可以使用Map<String, Map<String, Integer>>,例如:

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);

答案 4 :(得分:0)

我认为这就是你想要的,它支持整个密钥和搜索活动的唯一部分,请检查输出:

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);
    }
}

输出:

-------football------
football game
-------game------
basketball game
football game
-------basketball------
basketball game
-------football game------
football game