fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.stream.Collectors ;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. int targetCodePoint = "z".codePointAt( 0 ); // Get the code point assigned to the letter “z”, 122.
  15. List < String > inputs = List.of( "computers" , "information systems" , "zebra" );
  16. List < String > hits = new ArrayList <>( inputs.size() );
  17.  
  18. // Loop each input.
  19. loopingInputs:
  20. for ( String input : inputs )
  21. {
  22. // Loop the code point of each character.
  23. int[] codePoints = input.codePoints().toArray();
  24. for ( int codePoint : codePoints )
  25. {
  26. if ( codePoint == targetCodePoint ) // If this character is our target letter “z” (they share the same code point number), then we have a match. Remember this word, and bail out of inner loop.
  27. {
  28. hits.add( input );
  29. break loopingInputs;
  30. }
  31. }
  32. }
  33.  
  34. System.out.println( "hits = " + hits );
  35.  
  36. // Alternate approach.
  37. System.out.println(
  38. .of( "computers" , "information systems" , "zebra" )
  39. .stream()
  40. .filter(
  41. input -> input.codePoints().anyMatch( codePoint -> codePoint == "z".codePointAt( 0 ) )
  42. )
  43. .collect( Collectors.toList() ) // Or `.toList()` in later versions of Java.
  44. );
  45. }
  46. }
Success #stdin #stdout 0.13s 55092KB
stdin
Standard input is empty
stdout
hits = [zebra]
[zebra]