如何在 Java 7 中构建一个具有多个值的键的映射

发布于 2025-01-14 10:06:44 字数 132 浏览 1 评论 0原文

我想建立一个基于 2 个数组的地图,其中 1 个键里面有很多对象。

键:“字母 A”值:“信天翁” 值:“鳄鱼”

密钥:“字母 B” 值:“獾” 值:“Bandicoot”

该结构必须显示密钥 1 次,不重复

I want to build up a map based on 2 arrays where 1 key has many objects inside it.

Key: "Letter A" Value: "Albatross"
Value: "Alligator"

Key: "Letter B" Value: "Badger"
Value: "Bandicoot"

The structure must show the key 1 time, without repetitions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2025-01-21 10:06:44

希望代码是不言自明的。

Java 7:

public Map<String, List<String>> group(String[] input) {
        Map<String, List<String>> result = new HashMap<>();

        for (String str : input) {
            String key = "Letter " + str.charAt(0);
            if (result.containsKey(key)) {
                result.get(key).add(str);//if Key already exists, just add this word to existing list.
            } else {
                List<String> list = new ArrayList<>();
                list.add(str);
                result.put(key, list); //Otherwise, create a new list and add the new word into the list
            }
        }
        return result;
    }

Java 8:

public static Map<String, List<String>> group(String[] input) {
    return Arrays.stream(input)
            .collect(Collectors.groupingBy(k -> "Letter " + k.charAt(0)));    
            //Provide the key for how you want to group. In your case it is first character of string.
}

Hope the code is self explanatory.

Java 7:

public Map<String, List<String>> group(String[] input) {
        Map<String, List<String>> result = new HashMap<>();

        for (String str : input) {
            String key = "Letter " + str.charAt(0);
            if (result.containsKey(key)) {
                result.get(key).add(str);//if Key already exists, just add this word to existing list.
            } else {
                List<String> list = new ArrayList<>();
                list.add(str);
                result.put(key, list); //Otherwise, create a new list and add the new word into the list
            }
        }
        return result;
    }

Java 8:

public static Map<String, List<String>> group(String[] input) {
    return Arrays.stream(input)
            .collect(Collectors.groupingBy(k -> "Letter " + k.charAt(0)));    
            //Provide the key for how you want to group. In your case it is first character of string.
}
独孤求败 2025-01-21 10:06:44

您可以使用 Guava 的 Mutlimap 实现,但这可能与 Java 7 不兼容。 https://guava.dev/releases/ 23.0/api/docs/com/google/common/collect/Multimap.html

您可以通过使用地图中的值列表来获得相同的效果,如下所示:

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

然后,假设您想要的每个条目添加映射中的键位于 key 中,值位于 val 中,如下所示添加:

List<String> list = map.get(key);
if (list == null) {
  list = new ArrayList<>();
  map.put(key, list);
}
list.add(val);

You can use Guava's Mutlimap implementation, however that may not be Java 7 compatible. https://guava.dev/releases/23.0/api/docs/com/google/common/collect/Multimap.html

You can get the same effect by using a List for the values in your map like so:

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

Then, let's say for each entry you want to add to the map you have the key in key and value in val, add it like so:

List<String> list = map.get(key);
if (list == null) {
  list = new ArrayList<>();
  map.put(key, list);
}
list.add(val);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文