Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1.   static boolean isNumeric(String string) {
  2.     return string
  3.         .chars()
  4.         .allMatch(x -> '0' <= x && x <= '9');
  5.   }
  6.  
  7.   static int[] veryGeneralFunction(List<String> strings) {
  8.     return strings
  9.         .stream()                           // transform into stream
  10.         .filter(x -> isNumeric(x))          // get only the numeric elements
  11.         .sorted()                           // sort them
  12.         .mapToInt(x -> Integer.parseInt(x)) // transform each element to int
  13.         .toArray();                         // make an array
  14.   }
  15.  
  16.   public static void main(String[] args) {
  17.     List<String> strings = Arrays.asList("123", "a", "0", " ", "999", "9a9");
  18.     for (int i : veryGeneralFunction(strings)) {
  19.       System.out.println(i);
  20.     }
  21.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement