Java IntStream filter to calculate factors

Description

Java IntStream filter to calculate factors


import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class Main {
  public static void main(String[] argv) {
    List<Integer> list = getProperDivisors(24);

    System.out.println(list);//from  w  w w. j  a  v  a 2  s.co  m
  }

  public static List<Integer> getProperDivisors(int number) {
    List<Integer> properDivisors = new ArrayList<>();

    IntStream.range(1, (number / 2) + 1)
             .filter(divisor -> number % divisor == 0)
             .forEach(i -> properDivisors.add(i));

    return properDivisors;
  }
}



PreviousNext

Related