0

Possible Duplicate:
removing value from the list

I have a query I have a Map like this

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

and it contains the value like this

  1  22 23 24 25

so 1 is the key and the values are 22,23,24,25

Now I want to change the structure

like this

 1 22 23 24 25 | M

as shown above that in list apart from 22,23,24,25 I want to store pipe |M also please advise how to achieve this

4

4 回答 4

1

你可以只使用((List<String>)ab.get("1")).add("|M")

编辑:修改为<Object, Object>. 请注意,这是一个未经检查的演员表。当您知道Map要包含的内容时,您不应该使用原始类型。避免未经检查的强制转换的另一种方法是使用instanceof运算符。

于 2013-01-23T07:55:40.537 回答
0

因此,基于 Swapnil 的回答:

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

List<String> valueList = ab.get("1");
if (valueList == null) {
  valueList = new ArrayList<String>();
  ab.put("1",valueList);
}
valueList.add ("|M");
于 2013-01-23T08:35:06.767 回答
0

尝试这个

Map ab = new HashMap<String, List<String>>();
        ab.put("1", "22 23 24 25");
        Iterator it = ab.entrySet().iterator();

            if (ab.containsKey("1")) {
                String value = (String) ab.get("1");
                value += "| M";
                ab.put("1", value);
            }

        System.out.println(ab.get("1"));
于 2013-01-23T08:19:36.603 回答
-1

使用值创建一个新列表并使用相同的键将其放在地图上,它将被替换。

于 2013-01-23T07:55:59.257 回答