desislava_topuzakova

06. Generic Count Method Double

Oct 18th, 2020
1,796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package GenericCountMethodDouble_06;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class GenericBox<T extends Comparable<T>> {
  7.     private List<T> box;
  8.  
  9.  
  10.     public GenericBox() {
  11.         this.box = new ArrayList<>();
  12.  
  13.     }
  14.  
  15.     public void add(T element) {
  16.         this.box.add(element);
  17.     }
  18.  
  19.     public long count(List<T> list, T strToCount) {
  20.         long count = list.stream().filter(e -> e.compareTo(strToCount) > 0).count();
  21.         return count;
  22.     }
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29. package GenericCountMethodDouble_06;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Scanner;
  34.  
  35. public class Main {
  36.     public static void main(String[] args) throws NoSuchMethodException {
  37.         Scanner scanner = new Scanner(System.in);
  38.  
  39.         int n  = Integer.parseInt(scanner.nextLine());
  40.         GenericBox<Double> box = new GenericBox<>();
  41.         List<Double> list = new ArrayList<>();
  42.         double line;
  43.         for (int i = 0; i < n; i++) {
  44.             line = Double.parseDouble(scanner.nextLine());
  45.             //box.add(line);
  46.             list.add(line);
  47.         }
  48.  
  49.         double strToCount = Double.parseDouble(scanner.nextLine());
  50.         long count = box.count(list, strToCount);
  51.         System.out.println(count);
  52.  
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment