Ejemplo n.º 1
0
  @Test
  public void testCollectors() {

    ArrayList<Person> people = new ArrayList<>();
    ArrayList<Integer> things = new ArrayList<>();
    ArrayList<Employee> employees = new ArrayList<>();
    ArrayList<Student> students = new ArrayList<>();

    // Accumulate names into a List
    List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());

    // Accumulate names into a TreeSet
    Set<String> list2 =
        people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));

    // Convert elements to strings and concatenate them, separated by commas
    String joined = things.stream().map(Object::toString).collect(Collectors.joining(", "));

    // Find highest-paid employee
    Optional<Employee> highestPaid =
        employees.stream().collect(Collectors.maxBy(Comparator.comparingInt(Employee::getSalary)));

    // Group employees by department
    Map<Department, List<Employee>> byDept =
        employees.stream().collect(Collectors.groupingBy(Employee::getDepartment));

    // Find highest-paid employee by department
    Map<Department, Optional<Employee>> highestPaidByDept =
        employees
            .stream()
            .collect(
                Collectors.groupingBy(
                    Employee::getDepartment,
                    Collectors.maxBy(Comparator.comparingInt(Employee::getSalary))));

    // Partition students into passing and failing
    Map<Boolean, List<Student>> passingFailing =
        students.stream().collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
  }
Ejemplo n.º 2
0
 /**
  * Returns a vector of the maximum value of each column in the data frame
  *
  * @return a vector of the maximum value of each column in the data frame
  */
 default Vector max() {
   return collect(
       Object.class,
       org.briljantframework.data.Collectors.withFinisher(
           Collectors.maxBy(ObjectComparator.getInstance()), Optional::get));
 }