简体   繁体   English

从列表中删除值

[英]removing value from the list

I have a query I have a Map like this 我有一个查询,我有一个这样的地图

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

and it contains the value like this 它包含这样的值

   1  22 23 24 25

so 1 is the key(patient id) and the values(scriptinfo) are 22,23,24,25 所以1是键(患者编号),值(scriptinfo)是22,23,24,25

Now I am getting this value from back end as shown below 现在我从后端获取此值,如下所示

String ScriptInfo = ((IItemPluRx)item).getScriptInfo();

Now suppose the value of the script info is 24 现在假设脚本信息的值为24

I want to know whether in my Map in the values section that is in list, does it contain 24 and if it contains then I want to remove it from list please advise how to achieve it 我想知道列表中值部分的Map中是否包含24,如果包含,那么我想从列表中删除它,请告知如何实现

ppvValidatedinfo.get("1").remove("24");

it assumes that ppvValidatedinfo is generic 它假定ppvValidatedinfo是通用的

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

otherwise 除此以外

((List)ppvValidatedinfo.get("1")).remove("24");

note that it may throw a NullPointerException if the key is not in the map, which can be corrected as 请注意,如果键不在地图中,则可能会抛出NullPointerException,可以将其更正为

List<String> list = ppvValidatedinfo.get(key);
if (list != null){
   list.remove(value);
}

您将需要遍历HashMap,拉出List对象,然后对其进行遍历,如果找到搜索值,则将删除具有相应键的HashMap条目。

You can simply do it. 您可以简单地做到这一点。 from remove 删除

Removes the first occurrence of the specified element from this list, if it is present. 如果存在指定元素,则从该列表中删除该元素的第一次出现。 If the list does not contain the element, it is unchanged. 如果列表不包含该元素,则它保持不变。 More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). 更正式地讲,删除索引i最低的元素,使得(o == null?get(i)== null:o.equals(get(i)))(如果存在这样的元素)。 Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call). 如果此列表包含指定的元素(或者等效地,如果此列表由于调用而更改),则返回true。

List<String> value =  ppvValidatedinfo.get("1");
if(value != null){
   boolean deleted = value.remove("24");
}

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

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