I am looking for interesting solutions for this problem :
String key = "1;2;3;4";
String value = "Value1;Value2;Value whitespace;"
Now ';' devides each value from another. The same symbol ';' devides the keys also.
Now I want to end up with :
{"1" : "Value1", "2" : "Value2", "3" : "Value whitespace", "4" : null}
Of course if the values were more then the keys then the null should be no the left side of the pair (null: "Value5").
I made a pretty complecated solution to this problem using char arrays but is one big FOR with many cases and stuff.(it is O(n)). So I am curious to see a regex or substring solution or something that not includes big loop.
EDIT: Mine solution :
private List<ExampleObject> getExampleObjects(String key , String value) {
// s
if (key == null || value == null) {
return new ArrayList<ExampleObject>();
}
List<ExampleObject> exampleObjects = new ArrayList<ExampleObject>();
char[] keyToCharArray = key.toCharArray();
char[] valueToCharArray = value.toCharArray();
StringBuilder name = new StringBuilder();
StringBuilder value = new StringBuilder();
boolean nameCompleted = false;
boolean valueCompleted = false;
for (int i = 0, j = 0; i < keyToCharArray.length || j < valueToCharArray.length;) {
if (!nameCompleted) {
char a = ' ';
try{
a = keyToCharArray[i];
} catch(Exception e){
a = ';';
// throw : VALES and key not match. More key then value
//throw(e);
}
if (a == ';' ) {
nameCompleted = true;
} else if (!(i + 1 < keyToCharArray.length)){
name.append(a);
nameCompleted = true;
} else {
name.append(a);
}
i++;
}
if (!valueCompleted) {
char a = ' ';
try{
a = valueToCharArray[j];
} catch(Exception e){
a = ';';
// throw : VALES and key not match. More value then key
//throw(e);
}
if (a == ';') {
valueCompleted = true;
} else if(!(j + 1 < valueToCharArray.length)) {
value.append(a);
valueCompleted = true;
} else {
value.append(a);
}
j++;
}
if (nameCompleted && valueCompleted) {
exampleObjects.add(new ExampleObject(name.toString(), value.toString()));
name.setLength(0);
value.setLength(0);
nameCompleted = false;
valueCompleted = false;
}
}
return exampleObjects;
}
Where ExampleObject.class has fields key and value.
Java 8:
Try the following: (if you want to print a String as you said)
String#split()
value[]
and thekey[]
boolean
to indicate if a key or a value gets appendedStringBuilder
and loop through the length of the key[]String
usingStringBuilder#append()
Done. Try it before checking out the solution!
StringBuilder: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
String: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
My solution for printing out the String:
I've come up with a solution to your problem:
Output
Code
Another - slightly more elegant Java8 and Generic as possible.
A different point of view: don't do such things "manually".
What I mean is: instead of doing all "low level" operations yourself; you should abstract.
First, transform your key value strings into a Map. Like:
then finally, use some existing JSON library to simply generate a JSON representation based on that map.
In other words: unless you are talking about lists with millions of entries; do not worry about performance. Instead, worry about good abstractions, not re-inventing the wheel and code readability.
But if you really have to worry about performance, or memory aspects, then simply split into arrays, and then use those two arrays as input to some function that uses a StringBuilder to build the required output string.