【问题标题】:How to implement a HashMap from a string input如何从字符串输入实现 HashMap
【发布时间】:2014-11-07 02:56:51
【问题描述】:

我有一个数组列表,其中包含用户输入的一堆活动,每个活动都有一个用户输入的标题。我希望标题被解析并输入到哈希图中。现在用户可以执行搜索,他们可以在其中键入某些关键字。我需要使用哈希图来检查他们所指的数组列表中的哪些活动。

我遇到的问题是我知道我们不能有多个与特定键关联的值。例如,如果活动一的标题是:足球比赛,我希望足球 = 1 和比赛 = 1(这表明它与第一个活动相关),这样当用户输入其中一个词时,它会拉起这个活动。

【问题讨论】:

  • 你没有问问题。
  • 您的意思是,您的一个用户输入足球,另一个输入游戏,并且您希望活动是“足球比赛”,然后当任何用户使用“足球”或“比赛”时,他们能找到这个活动吗?那么有一个“篮球比赛”呢,用户在使用“比赛”搜索时是否应该同时获得?
  • 用户最初输入活动时,例如将标题足球比赛作为第一个活动。然后对于第二个活动,他可以输入篮球比赛。然后,当他执行搜索时,他可以键入,游戏,足球比赛和篮球比赛都会出现。如果用户输入足球比赛,只会显示 1 个。
  • @user4188409,请检查我的答案和输出,看看这是不是你想要的。

标签: java string arraylist collections hashmap


【解决方案1】:

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

【讨论】:

  • 如果键是连续整数,为什么还要使用HashMap?在这种情况下,您应该只使用ArrayList
  • 所以如果我有例如活动:1)足球比赛 2)曲棍球比赛 3)足球比赛。我首先使用 .get(game) ,这三个选项将是上面的 3 对吗?但是这次我可以使用 .get(soccer) 来缩小搜索范围吗?这将如何实施?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    如果是这样,您可以使用Map&lt;String, Map&lt;String, Integer&gt;&gt;,例如:

    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】:

      使用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_ids 的列表

      例子:

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

      这就是 HashMap 的样子

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

      【讨论】:

        【解决方案5】:

        我想这就是你想要的,它支持整个键和只有部分的活动来搜索,请检查输出:

        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
        

        【讨论】:

          猜你喜欢
          • 2022-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-18
          • 1970-01-01
          • 1970-01-01
          • 2014-03-27
          • 1970-01-01
          相关资源
          最近更新 更多