desislava_topuzakova

05. Generic Count Method String

Oct 18th, 2020
321
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package GenericCountMethodString_05;
  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. package GenericCountMethodString_05;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Scanner;
  32.  
  33. public class Main {
  34.     public static void main(String[] args) {
  35.         Scanner scanner = new Scanner(System.in);
  36.         int n  = Integer.parseInt(scanner.nextLine());
  37.         GenericBox<String> box = new GenericBox<>();
  38.         List<String> list = new ArrayList<>();
  39.         for (int i = 0; i < n; i++) {
  40.             String line = scanner.nextLine();
  41.             list.add(line);
  42.         }
  43.  
  44.         String strToCount = scanner.nextLine();
  45.         long count = box.count(list, strToCount);
  46.         System.out.println(count);
  47.     }
  48. }
  49.  
RAW Paste Data