【问题标题】:List to Map, with key as the first character and values as words corresponding to the first letter key characterList to Map,key为第一个字符,value为第一个字母key字符对应的单词
【发布时间】:2013-07-23 05:36:52
【问题描述】:

我有一个列表,例如。 {cat, ball, bat, cup, add, ant},我正在尝试将其转换为 Map,其中 key 作为 List 单词中可用的第一个字符,而 values 作为 List 中的相应单词。

a->add,ant

b->球,击球

c->猫,杯子

输出为地图。如果我能得到一些指导会很有帮助。

【问题讨论】:

  • 你有什么尝试吗?你有什么问题?
  • 有大写开头的单词吗?

标签: java list map


【解决方案1】:

使用
Map<Character, List<String>>
其中键是字符,值是该字符中所有单词的列表。

添加字符时,您必须记住“A”和“a”是不同的键。
获取第一个字符

  • 如果'A'和'a'被视为不同,那么

    key = .charAt(0)

  • 如果'A'和'a'被视为相同

    key = .toUpperCase().charAt(0)

【讨论】:

    【解决方案2】:

    使用纯Java:

    List<String> words = 
        new ArrayList(Arrays.asList("cat", "ball", "bat", "cup", "add", "ant"));
        Map<String, List<String>> map = new HashMap();
        for(String word: words){
            String firstChar = String.valueOf(word.charAt(0));
            if (map.get(firstChar) == null){
                map.put(firstChar, new ArrayList(Arrays.asList(word)));
            }
            else{
                map.get(firstChar).add(word);
            }
        }
    

    或者更好的方式使用 Guava 库,正如 Aniket Thakur 建议的那样

    【讨论】:

    • 只是想知道您为什么选择 key 作为字符串,因为我们可以选择 Character 作为 key
    【解决方案3】:

    指导

    使用Map&lt;String, List&lt;String&gt;&gt;substring(int, int) 方法

    【讨论】:

      【解决方案4】:

      你可以use Map&lt;String, List&lt;String&gt;&gt;,但我建议使用

      Multimap : Keys need not be unique.But same key can have same values.

      更好的是

      SetMultimap : Same as Multimap but no duplicate values for a given key. 
      Duplicate keys are allowed.
      

      所有这些类都在谷歌guava library

      例子

        Multimap<String, String> myMultimap = ArrayListMultimap.create();
      
        // Adding some key/value
        myMultimap.put("a", "add");
        myMultimap.put("a", "ant");
        myMultimap.put("b", "ball");
        myMultimap.put("b", "bat");
      

      请探索我上面提供的链接中的 API。

      【讨论】:

        【解决方案5】:

        在 java 8 中,我们可以用精确的代码解决这个问题:

        Map map = nameList.stream()
            .collect(
                Collectors
                    .toMap(x -> x.charAt(0),
                        x -> nameList.stream().filter(y -> y.startsWith(String.valueOf(x.charAt(0))))
                            .collect(Collectors.toList()),
                            (oldValue, newValue) -> oldValue, HashMap::new));
        

        【讨论】:

          【解决方案6】:

          如果您的列表中只有单词,这应该可以工作,以小写开头。

          public static Map<String, List<String>> arrayToList(String[] words) {
                  Map<String, List<String>> map = new HashMap<String, List<String>>();
                  for (int n = 97; n <= 122; n++) {
                      ArrayList<String> wordList = new ArrayList<String>();
                      for (int i = 0; i < words.length; i++) {
                          if (words[i].charAt(0) == n) {
                              wordList.add(words[i]);
                              map.put("" + words[i].charAt(0), wordList);
                          }
                      }
                  }
                  return map;
              }
          

          【讨论】:

          • 为什么我们需要 words[i].toCharArray() 因为我们只需要第一个字符有一个叫做 charAt(index) 的函数。
          • 但仍然需要再次调用 words[i].charAt(0),您可以使用 temp char。你能计算一下你的代码的复杂性吗?你不需要使用 2 for 循环。如果您移动 map.put("" + words[i].charAt(0), wordList); 会产生这种影响在你的 for 循环之外。
          • shevchik的解决方案比我的好太多了,看看吧。
          猜你喜欢
          • 1970-01-01
          • 2018-12-19
          • 1970-01-01
          • 2015-12-29
          • 1970-01-01
          • 2021-11-14
          • 1970-01-01
          • 1970-01-01
          • 2012-06-27
          相关资源
          最近更新 更多