Advertisement
neo7bf

java streams

Jan 27th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.stream.Collectors;
  3. public class HelloWorld{
  4.  
  5.      public static void main(String []args){
  6.          
  7.          try {
  8.              List<Integer> numbersWith3Digits = quiz(List.of(22,155,228,3,4555));
  9.              numbersWith3Digits.forEach(System.out::println);
  10.          }catch(Exception e)
  11.          {
  12.              System.out.println(e);
  13.          }
  14.      
  15.      }
  16.      
  17.      public static List<Integer> quiz(List<Integer> lista) throws Exception {
  18.          List<Integer> numbersWith3Digits = lista.stream().filter(e -> e.toString().length() > 2).collect(Collectors.toList());
  19.        
  20.         if(numbersWith3Digits.isEmpty()) {
  21.             throw new Exception ("not any");
  22.         }
  23.         return numbersWith3Digits;
  24.      }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement