JAVA - eclipse debug remove/change a key in map - java

I have a situation where I need to change(remove will also work) value corresponding to a key in a Map.
There are so many pairs in map. I don't want to copy all and create new map in change value for Map.
Is there any way I can directly change/remove value corresponding to a key.
I have tried changing complete map like :
Map m = commandParameters;
m.put("AID","");
return m;
But commandParameters is not resolved.
I tried changing that particular entry using random expressions, but could not work out.
Is there any way to do so?
**EDIT : ** commandParameters is original map.

Just do a remove on the map for a specific key.
commandParameters.remove("AID");
commandParameters.put("AID", "newvalue");
return commandParameters;

Related

"Size or zero" of value of multivalued hashmap

I have a multivalued Hashmap (technically a LinkedHashMap):
private LinkedHashMap<String, ArrayList<BodyPart>> bodyParts = new LinkedHashMap<>();
I want to find the number of values associated with a given key. However, bodyParts.get("sample key") returns null if the key isn't present, whereas I want it to return 0 (as there are zero values associated with that key).
I could shield it in an if statement:
int numberOfValues;
if(bodyParts.containsKey("sample"){
numberOfValues = bodyParts.get("sample").size();
}
but I was wondering if there is an easier/better way to do it? I've read the documentation for computeIfPresent but, truthfully, didn't really understand it.
Use Map.getOrDefault(Object key, V defaultValue).
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
You can use getOrDefault method of Java Map interface.
It allows you to set default value that is to be returned in case value corresponding to key is not found. So in use case mentioned above you can use :
numberOfValues = bodyParts.getOrDefault("sample", new ArrayList<BodyPart>()).size();

Freemarker - access value at a particular key in map

I have Map variable in my Freemarker template. How can I fetch a value at a particular key in the map, as we do in Java (map.get(<key>)).
I know how to iterate through keys and values of a map in a FTL. But I want a solution without iteration, on the lines of Java get() method of Map interface.
map[dynamicKey] or map.staticKey if the key is a string. Due to historical limitations, if the key isn't a string, map?api.get(nonStringKey).

Android HttpUrlConnection send multiple parameters with same key

How can i add multiple values with the same name in a HttpUrlConnection request.
example:
HashMap<String, String> params = new HashMap<>();
params.put("key[]", value1)
params.put("key[]", value2)
If i try to add multiple values with the same in postman i works fine, the application will send only one values (depends on request property, URLConnection setRequestProperty vs addRequestProperty).
I want to add both values as a parameter with the same name
This is not possible with Maps or HashMaps.
Taken from Oracles documentation on Maps:
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
The put command will replace the previous value associated with the given key in the map (you can think of this like an array indexing operation for primitive types).
The Oracle Documentation for put states:
Associates the specified value with the specified key in this map. If
the map previously contained a mapping for the key, the old value is
replaced.
Returns the previous value associated with key, or null if there was
no mapping for key.
This can be found here:
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put%28K,%20V%29
Alternatively You can do this and it will work fine.
You can make a JSONArray like this
JSONArray array = new JSONArray();
array.put("value1");
array.put("value2");
//and then you can send them as parameter like this-
params.put("key", array.toString());
It is not possible with params.put() But it is possible with params.add()
Reference : Difference between RequestParams add() and put() in AndroidAsyncHttp

How to Update value in MultiValueMap for a specific key

I have a MultiValueMap like
{3=[c-2, c-2], 2=[b-1, b-1], 1=[a-1, a-2, a-3]}
At one point I have to update a single value of a specific key
for example I have to update the key 2 like
2=[u-1,u-2]
how can i do this?
I've never used that library - but I would expect these two examples to do what you need:
multiMap.getCollection(2).clear();
multiMap.putAll(2, Arrays.asList("u-1", "u-2"));
Or
Collection c = multiMap.getCollection(2);
c.clear();
Collections.addAll(c, "u-1", "u-2");
The safest way is to call getCollection() to retrieve the current mapping, remove(key) to clear that mapping, iterate the retrieved collection to re-insert values that you want to keep, and/or add the new values.
If you know the type of collection used for a mapping (because you've called the constructor that takes collectionFactory), you could get the collection and update it directly.

How to access a hashmap in XSLT

I have some java code which returns me a map, and I want to access this in XSLT.
I just saved the result in some variable called $map.
Tried accessing the value as key($map,'key1') with no luck.
where $map is what some java method returned and key1 is the key of one map entry.
I want to fetch its value.
Any suggestions ?

Categories

Resources