How to obtain particular value from Map in a "String" format - java

I have a Map :
Map<String, String> value
How to obtain particular value "String" ----> "type" from json value, how do i obtain it?
I tried below code, which only returns me KEY and not VALUE
Iterator it = payload.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
String key = pair.getKey().toString();
String value = pair.getValue().toString();
Log.d("getActionValue", "key : " + key);
Log.d("getActionValue", "value : + value");
}

You don't access your variable in your output, since value is still in in the string.
Change like this:
Log.d("getActionValue", "value : " + value);

Your problem is here Log.d("getActionValue", "value : + value"); it should be Log.d("getActionValue", "value : "+ value);
Try this for loop :-
for (Map.Entry<String, String> entry : payload.entrySet())
{
Log.d("getActionValue", "key : " + entry.getKey());
Log.d("getActionValue", "value :" + entry.getValue());
}

I want that particular "String" ----> "type" from value, how do i
obtain it?
payload Map contains JSONObject's as value of every key. so to get type need to first convert String to JSONObject then get value using type key:
JSONObject jsonObject=new JSONObject(value);
String strType="";
if(jsonObject.has("notification-action")){
JSONObject jsonObjectnotification=jsonObject.optJSONObject
(""notification-action"");
if(jsonObjectnotification.has("type")){
strType=jsonObjectnotification.optString("type");
}
}

You logging it wrong.
Should be:
Log.d("getActionValue", "value : " + value);

Related

Java hashmap iterate only certain keys

I'm looking for a way to get the values for just a specific key when I get into the second loop here. I'm using snakeyaml and loading it to a Map. My yaml looks something like this:
number:
id: status.number
label: Number
contactType:
id: status.contact_type
label: Contact Type
What I'm attempting to do is just get the key and value for id. It's probably super obvious, but I haven't found a way to do so.
Map<String, Map<String, String>> a = (Map<String, Map<String, String>>) yaml.load(input);
for (Map.Entry<String, Map<String, String>> t : a.entrySet()) {
String key = t.getKey();
for (Map.Entry<String, String> e : t.getValue().entrySet()) {
System.out.println("OuterKey: " + key + " InnerKey: " + e.getKey() + " VALUE:" + e.getValue());
}
}
To get the value(s) for the "inner key", you don't need to loop through every inner map.
In the following example, I assume you already have an innerKey variable that holds the desired inner key.
Map<String, Map<String, String>> a = (Map<String, Map<String, String>>) yaml.load(input);
for (Map.Entry<String, Map<String, String>> t : a.entrySet()) {
String outerKey = t.getKey();
String innerValue = t.getValue().get(innerKey);
System.out.println("OuterKey: " + outerKey + " InnerKey: " + innerKey + " VALUE:" + innerValue);
}

build Uri string into name-value collection in java

HashMap<String, List<String>> filterableMap = new HashMap<>();
filterableMap.put("department", Arrays.asList("A","B",null));
filterableMap.put("group", Arrays.asList("C","D",null));
From the above map i need to dynamically build a queryString like show below.
"SomeURL"/token/filter?department=A&department=B&group=C&group=D
I just used Hashmap we can use any thing as long as we can hold the values in name value pair.
for (Map.Entry<String, List<String>> entry : filterableMap.entrySet()) {
String key = entry.getKey();
List<String> value = entry.getValue();
for(String aString : value){
System.out.println("key : " + key + " value : " + aString);
if("department".equalsIgnoreCase(key)) {
qStrBulderBuilder.append("department =");
qStrBulderBuilder.append(value);
}
}
}
I am using like above approach , but i need to make sure i need put "=" and "&" in the right places some times we may not get "department" or "group"

Iterate through nested map

I have a nested hashmap (Hashmap inside a hashmap).
Map<String,Map<String,String>> test = new HashMap<String,Map<String,String>>();
Map<String,String> testMp = new HashMap<String,String>();
testMp.put("1", "Key1");
testMp.put("2", "Key2");
testMp.put("3", "Key3");
testMp.put("4", "Key4");
testMp.put("5", "Key4");
test.put("One", testMp);
Ideally if I need to print the key and the values in the test map, I'd do this -
for(Map.Entry<String, Map<String,String>> t:test.entrySet()) {
System.out.println("KEY: " +t.getKey()+ " VALUE:" +t.getValue());
}
But I want the key and the value of the inner map as well. I want something like this -
Key of outermap
Key of innermap, and its value.
Then do a nested loop :
for(Map.Entry<String, Map<String,String>> t:test.entrySet()) {
String key = t.getKey();
for (Map.Entry<String,String> e : t.getValue().entrySet())
System.out.println("OuterKey: " + key + " InnerKey: " + e.getKey()+ " VALUE:" +e.getValue());
}
or
for(Map.Entry<String, Map<String,String>> t:test.entrySet()) {
System.out.println(t.getKey());
for (Map.Entry<String,String> e : t.getValue().entrySet())
System.out.println("KEY: " + e.getKey()+ " VALUE:" +e.getValue());
}
Write a recursive method which will call itself when it encounters that the entry to corresponding key is a map.

retrieve values after iteration

After iterating over a "hashmap list", I want to write the values into a string as 'value 1' and 'value 2'. I cannot figure how?Can someone help me!
for (Object listItem : (List)value) {
System.out.println(key + ":" + listItem);
I have 2 values in my data. The above code gives me
Con:Name
Con:ID
Now, I want 'Name' to be value 1 and 'ID' to be value 2 so that I can replace and write them in the following string
"xxxxxxxxxx"+key+"xxxx"+value 1+"xxxxxxxxx"+value 2+"xxxxxxxxxx";
Try:
String value1="";
String value2="";
int counter=0;
for (Object listItem : (List)value) {
System.out.println(key + ":" + listItem);
if(counter==0) {//first pass assign value to value1
value1=listItem;
counter++;//increment for next pass
}else if(counter==1) {//second pass assign value to value2
value2=listItem;
counter++;//so we dont keep re-assigning listItem for further iterations
}
}
System.out.println(value1);//should display 'Name'
System.out.println(value2);//should display 'ID'
Using the StringBuilder class (assuming key is defined outside of the loop)
StringBuilder sb = new StringBuilder("xxxxxx" + key + "xxxx");
for (Object listItem : (List)value) {
System.out.println(key + ":" + listItem);
sb.append(listItem+"xxxxxxxxx");
}

Request.getParameterMap values not castable to string

i am trying to get the complete parameter map from the request object and iterate over it.
here is the sample code
Map map = request.getParameterMap();
for(Object key : map.keySet()){
String keyStr = (String)key;
Object value = map.get(keyStr);
System.out.println("Key " + (String)key + " : " + value);
}
output
Key businessunit : [Ljava.lang.String;#388f8321
Key site : [Ljava.lang.String;#55ea0889
Key startDate : [Ljava.lang.String;#77d6866f
Key submit : [Ljava.lang.String;#25141ee0
Key traffictype : [Ljava.lang.String;#4bf71724
its evident from the output that the value object is an instance of String
now when i change my code to something like this
Map map = request.getParameterMap();
for(Object key : map.keySet()){
String keyStr = (String)key;
Object value = map.get(keyStr);
if(value instanceof String)
System.out.println("Key " + (String)key + " : " + (String)value);
}
it prints nothing but as per the previous output it should have printed the values and if i remove instanceOf check it gives ClassCastException. is this the expected behavior or i am doing something wrong here ?
[Ljava.lang.String;#XXXXXXX means it is array of String not a single String. So your condition fails and it does not print anything.
As the object which is returned is an array of strings as Harry Joy pointed out, you will have to use the Arrays.toString() method in order to convert that array to a printable string:
Map map = request.getParameterMap();
for (Object key: map.keySet())
{
String keyStr = (String)key;
String[] value = (String[])map.get(keyStr);
System.out.println("Key" + (String)key + " : " + Arrays.toString(value));
}
The value is an array. If you're sure that the array is not empty, you should get the string value like this:
String value = (String) map.get(keyStr)[0];

Categories

Resources