简体   繁体   English

访问Java映射内的ArrayList

[英]Access an ArrayList inside a Java Map

This is the first question I've posted on this site! 这是我在此网站上发布的第一个问题!

In the final part of a Java assignment for university, my class has been asked to show we understand how to use different types of arrays. 在大学的Java作业的最后一部分中,要求我的班级表明我们了解如何使用不同类型的数组。

At one point, we'll need to make and maintain a Map(key, value). 一方面,我们需要制作和维护一个Map(key,value)。 However, we'll need to use an ArrayList(String) as the Map's value. 但是,我们需要使用ArrayList(String)作为Map的值。

Adding to this Map appears easy enough: Just check that the Map in question does not already contain the specified key and then create the array with its values filled in and 'put' it, with the key String to the Map. 添加到此Map看起来很容易:只需检查所讨论的Map是否不包含指定的键,然后创建一个数组,将其值填充并“放入”该数组,并使用String到Map。

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

public void addToMap(String key, String value) {
  if (!this.map.containsKey(key) {
    ArrayList<String> array = new ArrayList<String>();
    array.add(value);
    this.map.put(key, array);
  }
  else {
    this.map.get(key).add(value); // Does this add a new item to the current ArrayList?
  }
}

But what if I wanted to edit the contents of that array? 但是,如果我想编辑该数组的内容怎么办? I've only just started looking at Maps today, so I'm a little new to them, but I see they have a 'put' method - and I understand what put, pop and push are usually for, so this is slightly familiar. 我今天才刚刚开始查看Maps,所以对他们来说还有些陌生,但是我看到他们有一种“放置”方法-而且我了解通常放置,弹出和推入的含义,因此有点熟悉。 Would I have to copy the contents of the array into a new temporary one, remove() the old "key => value" pair and make a new one with the additional data? 我是否需要将数组的内容复制到一个新的临时目录中,删除()旧的“ key => value”对,并使用其他数据创建一个新的数组?

Your help is very appreciated! 非常感谢您的帮助! :D :D

Your Map contains references to ArrayList s. 您的Map包含对ArrayList的引用。

String key = "some list name";
// This assumes this entry is in the map; doesn't check for null
ArrayList<String> listInMap = map.get(key);
listInMap.add("new thing to add to list");

When you use Map.get() it returns the reference to the ArrayList in the map, not a copy of it. 当您使用Map.get()它将返回对映射中ArrayList的引用,而不是其副本。 You can then modify that List via the returned reference. 然后,您可以通过返回的引用来修改该List

To edit the contents of one particular arrayList you need first to get it from the map, and then just add / remove from the array: 要编辑一个特定arrayList的内容,您首先需要从地图上获取它,然后只需从数组中添加/删除:

ArrayList<String> list = map.get("key");
list.get(index);
list.add("something");

From the Javadoc of Map.#put 来自Map的Javadoc 。#put

Associates the specified value with the specified key in this map (optional operation). 将指定值与该映射中的指定键关联(可选操作)。 If the map previously contained a mapping for the key, the old value is replaced by the specified value . 如果该映射先前包含该键的映射,则将旧值替换为指定的值 (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.) (仅当m.containsKey(k)返回true时,才认为映射m包含键k的映射。)

So you can see that if you modify your array , you don't need to delete the pair. 因此,您可以看到,如果您修改array ,则不需要删除该对。 Just re-map it with the previous key. 只需使用上一个键重新映射即可。

And There is an syntactical error in your code. 而且您的代码中存在语法错误。 You can't instantiate an interface. 您无法实例化接口。 So your code should be 所以你的代码应该是

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

or TreeMap or any direct concrete subclass of Map interface. TreeMapMap接口的任何直接具体子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM