bazzs
bazzs

Reputation: 147

how to remove java map value(not key) in java?

I have a java map with this key and values:

Xperia:[EM_A, EM_B, M-bus +, M-bus -, MB1, MB2, MB3, MB4, MB5, DOUT1, DOUT2, DOUT3, DOUT4, DOUT5, DOUT6, DOUT7, DOUT8]

I've tried this way but it not works:

final Iterator<Entry<String, ArrayList<String>>> iter = termekConf.devNameType.entrySet().iterator();
            while(iter.hasNext()) {
                  final Entry<String, ArrayList<String>> entry = iter.next();
                  if(entry.getValue().equals(torlendoValue)) {
                    iter.remove();                  
                  }
                }

how can I delete a value not a key? i really don't know it because the myMap.remove(key) just removes the hole key with values and i don't want it.

thank you!

Upvotes: 2

Views: 3560

Answers (3)

David
David

Reputation: 1770

myMap.devNameType.get('Key').remove('Value');

HashMap<String, ArrayList<String>> mMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> listA = new ArrayList<String>();
ArrayList<String> listB = new ArrayList<String>();
ArrayList<String> listC = new ArrayList<String>();
ArrayList<String> listD = new ArrayList<String>();

listA.add("1");
listA.add("2");
listA.add("3");
listA.add("5");

mMap.put("A", listA);
mMap.put("B", listB);
mMap.put("C", listC);
mMap.put("D", listD);


myMap.get("A").remove("3");

it removes the '3' value from key 'A'.

JavaDoc

Upvotes: 1

user2511414
user2511414

Reputation:

You cannot delete the key and keep the value persistence, because the implemented class for Map<K,V> interface doesn't allow a value without any references, in other word the size of both key and values should be same.

but you may set the key as null, first you need to utilize a Map which acceps null values and it's HashMap<K,V> (javadoc here) Example:

Map<String,Object> dic=new HashMap<>();
//filling the map
  for(String sx:dic.keySet()){
    if(<<cound the victim>>){
      synchronized(dic){//HashMap is not synchronized!
         Object ox=dic.get(sx);
         dic.remove(sx);
         dic.put(null,ox);
      }
    }
  }
}

EDIT : and for removing a value, so it's going to be same.

Map<String,Object> dic=new HashMap<>();
//filling the map
String[] keys=dic.keySet().toArray();int i=0;
  for(Object ox:dic.values()){
    if(<<cound the victim>>){
      synchronized(dic){//HashMap is not synchronized!
          String key=keys[i]
          dic.remove(key);
          dic.put(key,null);
      }
    }i++;
  }
}

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

how can i delete a value not a key?

I'm not sure why you would want to do this but you can't, AFAIK.

Possible solution

What you could do is change this value to an empty String or a default String value. Then when you want to look for "keys without a value" you can iterate over as you are and search for the empty String/default value.

To modify the current value, you should be able to use setValue. So something like

if(entry.getValue().equals(torlendoValue)) {
                entry.setValue("defaultString");
}

Upvotes: 1

Related Questions