0

I have a final string acceptedList which contains coma separated values.

final String acceptedList = "ABC, HIJ/LMN, PQR1, XYZ";

and another dynamic string receivedList which is also a csv, comes as input to my function.

String receivedList  = "ABC, PQR1";

If values in receivedList match the ones in acceptedList (in any order), it should return a success message.

Success Scenario examples:

receivedList  = "ABC";
receivedList  = "HIJ/LMN, ABC";
receivedList  = "XYZ, PQR1, ABC";

Failure Scenario examples:

receivedList  = "ABC, UNKNOWN";
receivedList  = "UNKNOWN";
receivedList  = "HIJ-LMN";

I have implemented the following code and it works fine. Is there a better way to implement this?

public class Driver {

    final static String acceptedList = "ABC, HIJ/LMN, PQR1, XYZ";

    public static void main(String[] args) {
        String receivedList  = "ABC, PQR1";
        System.out.println(checkValues(receivedList));      
    }

    private static String checkValues(String receivedList) {
        List<String> acceptedListArray = Arrays.asList(acceptedList.split(", "));
        String[] receivedArray = receivedList.split(", ");
        for (String string : receivedArray) {
            if (!acceptedListArray.contains(string)) {
                return "Error!";
            }
        }
        return "Success!";
    }
}

Output

Success!
1
  • 1
    If you can use library function provided by list then you don't need the loop. Commented Mar 19, 2021 at 6:57

2 Answers 2

3

Set#containsAll

I suggest you store the values in sets to avoid duplicate values being processed again and then use Set#containsAll to check if all the values in receivedList are present in acceptedList.

Demo:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Driver {

    final static String acceptedList = "ABC, HIJ/LMN, PQR1, XYZ";

    public static void main(String[] args) {
        String receivedList = "ABC, PQR1";
        System.out.println(checkValues(receivedList));

        receivedList = "ABC, UNKNOWN";
        System.out.println(checkValues(receivedList));
    }

    private static String checkValues(String receivedList) {
        Set<String> acceptedListSet = new HashSet<>(Arrays.asList(acceptedList.split(", ")));
        Set<String> receeivedListSet = new HashSet<>(Arrays.asList(receivedList.split(", ")));

        return acceptedListSet.containsAll(receeivedListSet) ? "Success!" : "Failure!";
    }
}

Output:

Success!
Failure!
Sign up to request clarification or add additional context in comments.

Comments

2

You may convert both CSV inputs to arrays using String#split. Then, convert them to lists and use List#containsAll to do the assertion:

public class Driver {
    final static String acceptedCSV = "ABC, HIJ/LMN, PQR1, XYZ";

    public static void main(String[] args) {
        String receivedCSV  = "ABC, PQR1";
        System.out.println(receivedCSV + " => " + checkValues(receivedCSV));      
        receivedCSV  = "ABC, DEF";
        System.out.println(receivedCSV + "  => " + checkValues(receivedCSV));      
    }

    private static String checkValues(String receivedCSV) {
        List<String> acceptedList = Arrays.asList(acceptedCSV.split("\\s*,\\s*"));
        List<String> receivedList = Arrays.asList(receivedCSV.split("\\s*,\\s*"));

        return acceptedList.containsAll(receivedList) ? "Success!" : "Error!";
    }
}

This prints:

ABC, PQR1 => Success!
ABC, DEF  => Error!

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.