I am new to Java. I have a problem that, I need to Implement the decode(String) method that decodes a String to a corresponding Map. In the assignment, the requirements are like this,
Empty keys and values are allowed, but the equals sign must be present (e.g. "=value", "key=").
If the key or value is empty, empty String should be returned.
If the given String is empty, an empty Map should be returned.
If the given String is null, null should be returned.
Sample Input: one=1&two=2
Should return a Map containing {"one": "1", "two": "2"}
Map<String, String> map = new HashMap<>();
map.put("One", "1");
map.put("Two", "2");
map.put("", "");
map.put("Key", "");
map.put("", "Value");
Set<String> keys = map.keySet();
for(String key : keys) {
System.out.print("\"" + key + "\"" + ":" + "\"" + map.get(key) + "\"");
}
My piece of code is giving output as required, But I have implemented this in the main method with Map<K, V> interface, while I need to write code that takes String as a parameter and decodes to Map.
Thanks
One solution could be:
public Map<String, String> parseMap(String mapString) {
if (mapString == null || mapString.isEmpty()) {
return Collections.emptyMap();
}
return Arrays.stream(mapString.split("&"))
.map(this::splitParam)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
}
public AbstractMap.SimpleEntry<String, String> splitParam(String it) {
final int idx = it.indexOf("=");
final String key = it.substring(0, idx);
final String value = it.substring(idx + 1);
return new AbstractMap.SimpleEntry<>(key, value);
}
usage
String inputString = "one=1&two=2";
Map<String, String> map = parseMap(inputString);
//your code to print the map again
Set<String> keys = map.keySet();
for(String key : keys) {
System.out.print("\"" + key + "\"" + ":" + "\"" + map.get(key) + "\"");
}
try this in your editor, just 4 lines :)
String input = "one=1&two=2";
String[] kvs = input.split("&");
Map<String, String> hashMap = Stream.of(kvs)
.collect(Collectors.toMap(item -> item.split("=")[0],
item -> item.split("=")[1]));
hashMap.forEach((k, v) -> System.out.println(k + ":" + v));
Related
This is my map:
Map<String, Object> params = new HashMap<>();
params.put("topicRef", "update-123456-78925-new-u1z1w3");
params.put("parentRef", "update-123456-78925-new-u1z1w3");
Script script = new Script(ScriptType.INLINE, "painless",
String.format("ctx._source.parentRef = params.parentRef; ctx._source.topicRef = params.topicRef"),
params);
request.setScript(script);
I want to convert my map into a string but I would like to change the pattern for e.g.:
"ctx._source.key = value;ctx._source.key = value"
I want to add to key value a prefix ctx._source.key and a suffix " =" (space and equal), then I would like to separate each entry with a semicolon.
String formattedMap = params.entrySet().
stream()
.map(e -> "ctx._source." + e.getKey() + " = " + e.getValue())
.collect(Collectors.joining(","));
Try something like this:
Map<String, String> yourMap = /*...*/;
StringBuilder bob = new StringBuilder();
yourMap.forEach((key, value) -> bob.append(key).append("=").append(value).append(";"));
String result = bob.toString();
If necessary you could remove the last ; on result via String.concat().
You could stream your Map's entries, then use the map operation to map each entry to the formatted String and ultimately join each element with the collect(Collectors.joining(";")) operation.
Map<String, Object> params = new HashMap<>();
params.put("topicRef", "update-123456-78925-new-u1z1w3");
params.put("parentRef", "update-123456-78925-new-u1z1w3");
String result = params.entrySet().stream()
.map(entry -> String.format("%s%s%s%s", "ctx._source.", entry.getKey(), " =", entry.getValue()))
.collect(Collectors.joining(";"));
System.out.println(result);
Here is a link to test the code
https://www.jdoodle.com/iembed/v0/rrK
Output
String result = params.entrySet()
.stream()
.map(x -> "ctx._source." + x.getKey() + " = " + x.getValue())
.reduce((x, y) -> x + ";" + y).get();
I want to convert a Stream of a Map<> into a String, to append it to a textArea. I tried some methods, the last with StringBuilder, but they don't work.
public <K, V extends Comparable<? super V>> String sortByAscendentValue(Map<K, V> map, int maxSize) {
StringBuilder sBuilder = new StringBuilder();
Stream<Map.Entry<K,V>> sorted =
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) sorted));
String read;
try {
while ((read=br.readLine()) != null) {
//System.out.println(read);
sBuilder.append(read);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sorted.limit(maxSize).forEach(System.out::println);
return sBuilder.toString();
}
You can collect the entries into a single String as follows:
String sorted =
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(e-> e.getKey().toString() + "=" + e.getValue().toString())
.collect(Collectors.joining (","));
Consider slight change to #Eran's code, with regard to the fact that HashMap.Entry.toString() already does joining by = for you:
String sorted =
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(Objects::toString)
.collect(Collectors.joining(","));
It is easy to do this, you can use the Steams API to do this. First, you map each entry in the map to a single string - the concatenated string of key and value. Once you have that, you can simply use the reduce() method or collect() method to do it.
Code snippet using 'reduce()' method will look something like this:
Map<String, String> map = new HashMap<>();
map.put("sam1", "sam1");
map.put("sam2", "sam2");
String concatString = map.entrySet()
.stream()
.map(element-> element.getKey().toString() + " : " + element.getValue().toString())
.reduce("", (str1,str2) -> str1 + " , " + str2).substring(3);
System.out.println(concatString);
This will give you the following output:
sam2 : sam2 , sam1 : sam1
You can also use the collect()' method instead ofreduce()` method. It will look something like this:
String concatString = map.entrySet()
.stream()
.map(element-> element.getKey().toString() + " : " + element.getValue().toString())
.collect(Collectors.reducing("", (str1,str2) -> str1 + " , " + str2)).substring(3);
Both methods give the same output.
I have written the following code:
String reqParams = null;
Map<String, String[]> params = request.getParameterMap();
for (Object key : params.keySet()) {
String keyStr = (String) key;
String[] value = params.get(keyStr);
reqParams = reqParams + ((String) key + "=" + Arrays.toString(value) + " ");
}
System.out.println(reqParams);
I have the following output:
nullorderId=[01] orderStatus=[delivered]
How can I get rid of the null that prints at the beginning?
Is it possible to avoid the [ ] from printing?
How can I do this using streams?
Use map operation and the joining collector:
String resultSet =
params.entrySet()
.stream()
.map(e -> e.getKey() + "=" + String.join(", ", e.getValue()))
.collect(Collectors.joining(" "));
", " is the delimiter if the array has more than one element.
" " in the joining is the delimiter to separate the elements
from the source.
Initialize reqParams as an empty string. That is,
String reqParams = null;
becomes
String reqParams = "";
Initialize reqParams like this : String reqParams = "" or don't append your string to redParams and just do this reqParams = ((String) key + "=" + value[0] + " ");
Just get the element in the array instead of turning the array into a String: reqParams = reqParams + ((String) key + "=" + value[0] + " ");
You could stream params.keySet().stream().forEach(key -> .... ) and add a function in the forEach that appends the key and the value to a String like you're doing in the for loop.
You could use the join method of the String class to turn the array into a String, avoiding the [ ] characters. Also, you don't need all the casting and extra parentheses that you have in your original code, if you use Map.Entry with type parameters, and iterate through the entry set instead of the key set.
for( Map.Entry<String,String[]> entry : params.entrySet()) {
reqParams = reqParams + entry.getKey() + "=" + String.join(",", entry.getValue()) + " ";
}
So here, join takes your array and separates all the elements with commas.
Lastly, if you initialise reqParams to "" instead of null, then the word null won't print.
String reqParams = "";
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);
}
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"