fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Arrays;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String[] names = {"123,456",
  14. "123,456,789",
  15. "456,789,123"};
  16. String lookingFor = "456";
  17.  
  18. int max = Arrays.stream(names)
  19. .mapToInt(name -> name.split(",").length)
  20. .max().getAsInt();
  21.  
  22. String[] found = Arrays.stream(names)
  23. .filter(name -> arrayContains(name.split(","), lookingFor))
  24. .filter(name -> name.split(",").length == max)
  25. .toArray(String[]::new);
  26.  
  27. System.out.println("found:");
  28. for(String foundName : found) {
  29. System.out.println(foundName);
  30. }
  31.  
  32. }
  33.  
  34. private static boolean arrayContains(Object[] array, Object item) {
  35. return Arrays.stream(array).anyMatch(arrayItem -> arrayItem.equals(item));
  36. }
  37.  
  38.  
  39. }
Success #stdin #stdout 0.08s 34524KB
stdin
Standard input is empty
stdout
found:
123,456,789
456,789,123