所以我有一个 HashMap ,它将字符串存储为键,将列表存储为值。
我需要在我的方法中创建一个新列表,用存储在 HashMap 中的列表填充我的新列表。
这样做我不知道如何将 HashMap 中列表的所有对象放入新列表中...
//Original hashmap
private Map<String, List<Object>> things;
//inside the method im trying to create...
List<Object> otherThings = new ArrayList<>();
//unsure what i need here:
otherThings.add....
新列表应将列表中的所有对象存储在 HashMap 中。
最佳答案
你可能想要这样的东西:
private Map<String, List<Object>> things;
List<Object> otherThings = new ArrayList<>();
otherThings.addAll(things.get("some_key"));
这会检索 HashMap 中与“some_key”匹配的列表,并将内容复制到 otherThings 的新列表中。请记住,仅复制列表,但对象引用仍然相同。
关于java - 如何获取存储在 HashMap 中的列表中的对象,以创建新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994146/