简体   繁体   中英

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

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

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

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

it assumes that ppvValidatedinfo is generic

 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

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). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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