0

In my business case I need to pick codes of cola brands which has assigned third party dealers with codes.

I need to crosscheck all the dealer codes currently in contract are correct and no outside code is entered. To do that I have master list of codes.

See the sample data I have

List<String> mixedOfLAndPCodes = ["L123", "P123", "P234", "", "P2345", ""];
List<String> masterPCodes = ["P123", "P234", "P2345", "P111", "P23456"];
String masterLCode = "L123";

How can I be sure that all mixedOfLAndPCodes are available in master code list with java stream?

Code I wrote is something like

boolean a = mixedOfLAndPCodes.stream().allMatch(it -> masterLCode.equals(it) || masterPCodes.contains(it));

Not able to figure out if that is enough to test my case or some improvements needed.

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
  • 1
    How do you want to handle empty Strings? Your current code will return false, due to the presence of 2 empty Strings in the mixedOfLAndPCodes list. Is that the desired outcome? – Eran Apr 16 '20 at 08:37
  • Having empty string is okay. It mean no cola assigned to it. – Kunal Vohra Apr 16 '20 at 08:46
  • In that case, you might want to filter out empty Strings from your Stream. Otherwise allMatch will return false. – Eran Apr 16 '20 at 08:47
  • Don’t you see any issues with code? – Kunal Vohra Apr 16 '20 at 08:49
  • there is an issue with List instantiation it will not work. you should use something like `List.of("L123", "P123", "P234", "", "P2345", "")`. beside that and the already mentioned handling of empty strings it should work. – pero_hero Apr 16 '20 at 08:52
  • Well fine. That was just sample data. Definitely not static. – Kunal Vohra Apr 16 '20 at 08:53

1 Answers1

0

I like to use Predicate when i want to give a functional prespective on why i'm writing a certain code.

We can for your case, create a Predicate for each of you need then combine them to give an overview. Here is an example :

   public static Predicate<String> isExistingPMaster(List<String> pCodes) {
        return pCodes::contains;
   }

    public static Predicate<String> isExistingLMaster(List<String> lCodes) {
        return lCodes::contains; 
     /*
      * here we manage a list of lCodes but it also could be 
      * a single string with an equals() as in your question.
      *
      */
    }

    public static Predicate<String> isExistingInMaster(List<String> pCodes, List<String> lCodes) {
        return isExistingPMaster(pCodes).or(isExistingLMaster(lCodes));
    }

When predicates are created we can use them directly in streams :

List<String> mixedOfLAndPCodes = Arrays.asList("L123", "P123", "P234", "", "P2345", "");
List<String> masterPCodes = Arrays.asList("P123", "P234", "P2345", "P111", "P23456");
List<String> masterLCode = Arrays.asList("L123", "L125");

mixedOfLAndPCodes.stream().filter(isExistingInMaster(masterPCodes, masterLCode));

It flows well and even non-developer reads at a certain level.

Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37