Advertisement
Jmdnbvs

Ejercicio modulo 3 Juan Fermin

May 18th, 2021
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7.            
  8. public class Ejemplo_1 {
  9.      
  10.     public List<Integer> lista = Arrays.asList(1, 2 , 3,4);
  11.     public List<String> names = new ArrayList<>();
  12.        
  13.      
  14.    
  15.     public Ejemplo_1() {
  16.         this.names.add("Juan");
  17.         this.names.add("Marco");
  18.         this.names.add("Fermin");
  19.         this.names.add("Sebastian");
  20.                 this.names.add("Simon");
  21.  
  22.     }
  23.    
  24.    
  25.     ///////Imperativa////////////////
  26.     public Integer operar() {
  27.         int total = 0;
  28.         for (int i = 0; i < lista.size(); i++) {
  29.             total += lista.get(i);
  30.         }
  31.         return total;
  32.     }
  33.        
  34.    
  35.     public List primeraLetra(List<String> nombres) {
  36.         List<String> empiezanPorZ = new ArrayList<>();
  37.         for (String nombre : nombres) {
  38.             if (nombre.charAt(0) == 'S' || nombre.charAt(0) == 's') {
  39.                 empiezanPorZ.add(nombre);
  40.             }
  41.         }
  42.         return empiezanPorZ;
  43.     }
  44.    
  45.    
  46.     //Declarativa  /// funcional
  47.     public int operarFuncional(List<Integer> numeros){
  48.         return numeros.stream().mapToInt(Integer::valueOf).sum();
  49.     }
  50.    
  51.     public List primeraLetraFuncional(List<String> nombres ){
  52.         List listaNombres = nombres.stream().filter(name -> name.startsWith("S")).collect(Collectors.toList());
  53.         return listaNombres;
  54.     }
  55.    
  56.  
  57.  
  58.  
  59.     public static void main(String[] args) {
  60.         Ejemplo_1 ejempl = new Ejemplo_1();
  61.        
  62.             //Imperativa
  63.             System.out.println(ejempl.operar());
  64.             System.out.println(ejempl.operarFuncional(ejempl.lista));
  65.  
  66.  
  67.             /// Declarativos || Funcional
  68.             System.out.println(ejempl.primeraLetra(ejempl.names));
  69.             System.out.println(ejempl.primeraLetraFuncional(ejempl.names));
  70.  
  71.     }
  72.  
  73.    
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement