向 HashMap 添加一个键而不添加值?

发布于 2024-11-08 20:28:00 字数 219 浏览 0 评论 0 原文

有没有一种方法可以在不添加值的情况下向 HashMap 添加键?我知道这看起来很奇怪,但我有一个 HashMap> 我希望首先能够根据需要创建键,然后检查某个键是否存在,如果因此,输入适当的值,即 ArrayList

这足够令人困惑了吗?

Is there a way to add a key to a HashMap without also adding a value? I know it seems strange, but I have a HashMap<String, ArrayList<Object>> amd I want to first be able to create keys as needed and then check if a certain key exists and, if so, put the appropriate value, namely the ArrayList<Object>

Was that confusing enough?

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

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

发布评论

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

评论(5

夜雨飘雪 2024-11-15 20:28:00

由于您使用的是Map>,因此您真的正在寻找multimap。我强烈建议使用第三方库,例如 Google Guava - 请参阅 Guava 的 Multimaps.

Multimap<String, Object> myMultimap = ArrayListMultimap.create();

// fill it
myMultimap.put("hello", "hola");
myMultimap.put("hello", "buongiorno");
myMultimap.put("hello", "สวัสดี");

// retrieve
List<String> greetings = myMultimap.get("hello");
                      // ["hola", "buongiorno", "สวัสดี"]

Java 8 更新:我不再相信每个 Map> 都应该重写为多重映射。如今,无需 Guava 即可轻松获得所需内容,这要归功于 Map#computeIfAbsent()

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

// fill it
myMap.computeIfAbsent("hello", ignored -> new ArrayList<>())
  .addAll(Arrays.asList("hola", "buongiorno", "สวัสดี");

// retrieve
List<String> greetings = myMap.get("hello");
                      // ["hola", "buongiorno", "สวัสดี"]

Since you're using a Map<String, List<Object>>, you're really looking for a multimap. I highly recommend using a third-party library such as Google Guava for this - see Guava's Multimaps.

Multimap<String, Object> myMultimap = ArrayListMultimap.create();

// fill it
myMultimap.put("hello", "hola");
myMultimap.put("hello", "buongiorno");
myMultimap.put("hello", "สวัสดี");

// retrieve
List<String> greetings = myMultimap.get("hello");
                      // ["hola", "buongiorno", "สวัสดี"]

Java 8 update: I'm no longer convinced that every Map<K, SomeCollection<V>> should be rewritten as a multimap. These days it's quite easy to get what you need without Guava, thanks to Map#computeIfAbsent().

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

// fill it
myMap.computeIfAbsent("hello", ignored -> new ArrayList<>())
  .addAll(Arrays.asList("hola", "buongiorno", "สวัสดี");

// retrieve
List<String> greetings = myMap.get("hello");
                      // ["hola", "buongiorno", "สวัสดี"]
给妤﹃绝世温柔 2024-11-15 20:28:00

我不确定你想这样做。您可以将 null 存储为键的值,但是如果您这样做,当您执行 .get("key") 时,如何才能知道该键是否存在存在还是确实存在但具有 null 值?无论如何,请参阅 文档

I'm not sure you want to do this. You can store null as a value for a key, but if you do how will be able to tell, when you do a .get("key") whether the key exists or if it does exist but with a null value? Anyway, see the docs.

假情假意假温柔 2024-11-15 20:28:00

您可以输入 null 值。 HashMap 允许

您也可以首先使用Set,并检查它的键,然后填充映射。

You can put null values. It is allowed by HashMap

You can also use a Set initially, and check it for the key, and then fill the map.

神经大条 2024-11-15 20:28:00

是的,这已经够令人困惑的了;)我不明白为什么你想要存储没有值的键,而不是仅仅放置空数组列表而不是null

添加 null 可能是一个问题,因为如果您调用

map.get("somekey");

并收到 null,那么您不知道该键是否未找到,或者它是否存在但映射到...

Yes, it was confusing enough ;) I don't get why you want to store keys without values instead just putting empty arraylists instead of null.

Adding null may be a problem, because if you call

map.get("somekey");

and receive a null, then you do not know, if the key is not found or if it is present but maps to null...

七颜 2024-11-15 20:28:00
//This program should answer your questions
import java.util.*;
public class attemptAddingtoHashMap { //Start of program

//MAIN METHOD #################################################

public static void main(String args[]) {  //main begins

Map<String, ArrayList<Object>> hmTrial = new HashMap<String, ArrayList<Object>>(); 

ArrayList alTrial = new ArrayList();//No values now

if (hmTrial.containsKey("first")) {
hmTrial.put("first", alTrial); }

else {hmTrial.put("first",alTrial);}
//in either case, alTrial, an ArrayList was mapped to the string "first"
//if you choose to, you can also add objects to alTrial later
System.out.println("hmTrial is " + hmTrial); //empty now
alTrial.add("h");
alTrial.add("e");
alTrial.add("l");
alTrial.add("l");
alTrial.add("o");
System.out.println("hmTrial is " + hmTrial);//populated now

   } //end of main
//#############################################################################################################

} //end of class

//Note - removing objects from alTrial will remove the from the hashmap
//You can copy, paste and run this code on https://ide.geeksforgeeks.org/
//This program should answer your questions
import java.util.*;
public class attemptAddingtoHashMap { //Start of program

//MAIN METHOD #################################################

public static void main(String args[]) {  //main begins

Map<String, ArrayList<Object>> hmTrial = new HashMap<String, ArrayList<Object>>(); 

ArrayList alTrial = new ArrayList();//No values now

if (hmTrial.containsKey("first")) {
hmTrial.put("first", alTrial); }

else {hmTrial.put("first",alTrial);}
//in either case, alTrial, an ArrayList was mapped to the string "first"
//if you choose to, you can also add objects to alTrial later
System.out.println("hmTrial is " + hmTrial); //empty now
alTrial.add("h");
alTrial.add("e");
alTrial.add("l");
alTrial.add("l");
alTrial.add("o");
System.out.println("hmTrial is " + hmTrial);//populated now

   } //end of main
//#############################################################################################################

} //end of class

//Note - removing objects from alTrial will remove the from the hashmap
//You can copy, paste and run this code on https://ide.geeksforgeeks.org/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文