fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. enum Rara {
  9. MA_ON("ST"),
  10. MA_MAN("ST", "YS"),
  11. MA_IP("ST", "YS", "IC"),
  12. CODE("RC");
  13.  
  14. Rara(String... codes) {
  15. this.codes = List.of(codes);
  16. }
  17.  
  18. private List<String> codes;
  19.  
  20. private static List<String> commonElements(List<String> list1, List<String> list2) {
  21. List<String> common = new ArrayList<>(list1); // to avoid modifying `list1`
  22. common.retainAll(list2); // leaves in `common` only elements from `list2`
  23. return common;
  24. }
  25.  
  26. public static Optional<Rara> getValue(List<String> values){
  27. return Arrays.stream(values())
  28. .map(en -> Map.entry(en, commonElements(en.codes, values).size()))
  29. .filter(entry -> entry.getValue()>0)
  30. .max(Comparator.comparing(Map.Entry::getValue))
  31. .map(Map.Entry::getKey);
  32. }
  33.  
  34. }
  35.  
  36. class Ideone {
  37. public static void main(String args[]){
  38. System.out.println(Rara.getValue(Arrays.asList("ST", "YS")));
  39. // Optional[MA_MAN]
  40.  
  41. System.out.println(Rara.getValue(Arrays.asList("ST")));
  42. // Optional[MA_ON]
  43.  
  44. System.out.println(Rara.getValue(Arrays.asList("ST", "RC")));
  45. // Optional[MA_ON]
  46.  
  47. System.out.println(Rara.getValue(Arrays.asList("STB", "RCC")));
  48. //Optional.empty
  49. }
  50. }
  51.  
Success #stdin #stdout 0.11s 48792KB
stdin
Standard input is empty
stdout
Optional[MA_MAN]
Optional[MA_ON]
Optional[MA_ON]
Optional.empty