0

I have a string whose value can be Insert, Insert&Update or Update. Now based on this value, i have to create a Map as below.

Case 1 : When myString="Insert", i need output like this

"Database": [
                {
                    "Action": "Insert"
                }
            ]

Case 2 : When myString="Insert&Update", i need output like this

"Database": [
                {
                    "Action": "Insert"
                },
                {
                    "Action": "Update"
                }
            ]

Case 1 : When myString="Update", i need output like this

"Database": [
                {
                    "Action": "Update"
                }
            ]

I'm new to java and based on my knowledge what i'm doing currently is manually checking the condition and creating the list manually(something like below). Is there any way that I can split myString and automatically create this list?

Map<String, String> database = new HashMap<String, String>();
List<Map<String, String>> subList = new ArrayList<>();              
if(myString.equals("Insert")){              
    database.put("Action", "Insert");
    subList.add(database);
} else if(myString.equals("Insert&Update")){
    database.put("Action", "Insert");
    subList.add(database);
    database = new HashMap<String, String>();
    database.put("Action", "Update");
    subList.add(database);
} else {
    ....
}

3 Answers 3

1

Stop working harder and start working smarter

Do this:

  1. Analyze the requirements. Upon analysis, this becomes clear: you are implementing a variation of FizzBuzz.
  2. Implement an appropriate solution.

Google search for "FizzBuzz" if you don't recognize the reference.

Here is the solution to your problem:

  1. Receive a string as input.
  2. If the input contains the word "Insert", add an insert entry to the output map.
  3. If the input contains the word "Update", add an update entry to the output map.
  4. Return the output map.
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can use myString.split("&") to get an array of actions. Then you can iterate over it and generate all actions:

List<Map<String, String>> actions = new LinkedList<>();
String[] strings = myString.split("&");
for (String s : strings) {
  actions.add(action(s));
}

using this helper function to generate the action dictionaries:

Map<String, String> action(String action) {
  Map<String, String> map = new HashMap<>();
  map.put("Action", action);
  return map;
}

Just bear in mind that if somebody submits myString=Messing&With&You this code isn't validating the input unlike your original code. So you could wrap your put in an if:

if (s.equals("Insert") || s.equals("Update")) {
  actions.add(action(s));
}

You might also want to throw an exception if your input is invalid:

if (actions.size() != strings.length) {
  throw new Exception("Inputs invalid");
}

Putting everything together:

Map<String, String> action(String action) {
  Map<String, String> map = new HashMap<>();
  map.put("Action", action);
  return map;
}

void handle(String myString) {
  List<Map<String, String>> actions = new LinkedList<>();
  String[] strings = myString.split("&");
  for (String s : strings) {
    if (s.equals("Insert") || s.equals("Update")) {
      actions.add(action(s));
    }
  }
  if (actions.size() != strings.length) {
    throw new Exception("Inputs invalid");
  }
}

Comments

1

Provided that your string has only one of the possible three values (otherwise add a filter), you can do it quite easily as follows

String myString = ... ;
    
List<Map<String, String>> subList = Arrays.stream(myString.split("&"))
                                          .map(str -> Collections.singletonMap("Action", str))
                                          .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.