Home > Back-end >  How to compare list of string with enum string values without Map or switch case?
How to compare list of string with enum string values without Map or switch case?

Time:11-26

In Enum i defined string values like

public enum Rara{
  MAG_STRIPE_ONLY("MGST"),
  MAG_STRIPE_MANUAL("MGST", "PHYS"),
  MAG_STRIPE_MANUAL_CHIP("MGST", "PHYS", "CICC"),
  BARCODE("BRCD");
  Rara(String... s){
       //What here?
  }
  public Rara getValue(List<Val> v){
        //What here?
  }
}

From front end i am getting data like

<val>MGST<val>
<val>PHYS</val>  <--- This shall return MAG_STRIPE_MANUAL from Enum

or we get data like

<val>MGST</val> <---- This shall return MAG_STRIPE_ONLY

Is there any tweak to do inside enum itself without using Map or switchcase but only with value parameters

so that i supply say List and it should return appropriate Enum Value?

CodePudding user response:

The following should work:

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public enum Rara {
    MAG_STRIPE_ONLY("MGST"),
    MAG_STRIPE_MANUAL("MGST", "PHYS"),
    MAG_STRIPE_MANUAL_CHIP("MGST", "PHYS", "CICC"),
    BARCODE("BRCD");

    Rara(String... codes) {
        this.codes = List.of(codes);
    }

    private List<String> codes;

    public static Optional<Rara> getValue(List<String> values){
        return Arrays.stream(values())
                .filter(rara -> rara.codes.stream().anyMatch(values::contains))
                .findFirst();
    }
}

You basically need to iterate over the list of codes for each Rara and check if they match any of the provided values. If they do you simply return the first one (Optional because you might find none). Without further requirements on what to do when to find multiple Rare matches or what to do if none is found, this is the best we can suggest to you.


Given that you want a full match between values and codes, you need to use another filter predicate as follows:

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public enum Rara {
    MAG_STRIPE_ONLY("MGST"),
    MAG_STRIPE_MANUAL("MGST", "PHYS"),
    MAG_STRIPE_MANUAL_CHIP("MGST", "PHYS", "CICC"),
    BARCODE("BRCD");

    Rara(String... codes) {
        this.codes = List.of(codes);
    }

    private List<String> codes;

    public static Optional<Rara> getValue(List<String> values){
        return Arrays.stream(values())
                .filter(rara -> rara.codes.containsAll(values))
                .findFirst();
    }
}

The code bellow shows that this is in fact working:

public class Main {
    public static void main(String args[]){
        System.out.println(Rara.getValue(Arrays.asList("MGST", "PHYS")));  
              // Optional[MAG_STRIPE_MANUAL]

        System.out.println(Rara.getValue(Arrays.asList("MGST")));  
              // Optional[MAG_STRIPE_ONLY]
    }
}

See this code run live at IdeOne.com.

CodePudding user response:

Only way is to iterate trough Rara.value and compare lists.

  • Related