Listing employees whose employee number is divisible by some given number Here I want to improve the code in addListofEmployees and mainMethod. How can I do so?

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class AddEmployees extends Employee {

    public static List&lt;Employee&gt; addListOfEmployees(long upperLimit) {

        List&lt;Employee&gt; list = new LinkedList&lt;&gt;();
        for (long i = 0; i &lt; upperLimit; i++) {
            list.add(new Employee(i));
        }
        return list;
    }

    public static List&lt;Employee&gt; getAllNumbersDividedByProvidedNumber(long upperLimit, int divideBy) {
        return AddEmployees.addListOfEmployees(upperLimit).parallelStream()
                .filter(Employee -&gt; Employee.getNumber() % divideBy == 0).collect(Collectors.toList());

    }

    public static void mainMethod() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            System.out.println("enter upper limit as lower is zero");
            long upperLimit = sc.nextInt();
            System.out.println("enter the number you want to divide with");
            int divideBy = sc.nextInt();

            for (Employee e : AddEmployees.getAllNumbersDividedByProvidedNumber(upperLimit, divideBy)) {
                System.out.println(e.getNumber());
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        } finally {
            if (sc != null)
                sc.close();
        }

    }

    public static void main(String[] args) {
        mainMethod();
    }
}

Employee Class

package com.lambada.filter_predicatemethod;

public class Employee {

    private long number;

    public Employee() {
    }

    public Employee(long number) {
        this.number = number;
    }

    public long getNumber() {
        return number;
    }

}


Edit

I thinks this can achieving same result but not sure that chaining is a good part

public static List&lt;Employee&gt; getEmployeeDividedBy(long upperLimit, long dividedby) {
        return LongStream.range(0, upperLimit).mapToObj(Employee::new).collect(Collectors.toList()).stream()
                .filter(Employee -&gt; Employee.getNumber() % dividedby == 0).collect(Collectors.toList());
    }
Yes, there is.

AddEmployee.java:

private AddEmployees() {}

public static List&lt;Employee&gt; createListOfEmployees(long upperLimit) {
    return LongStream.range(0, upperLimit)
                     .mapToObj(Employee::new)
                     .collect(Collectors.toList());
}

public static List&lt;Employee&gt; getAllNumbersDividedByProvidedNumber(long upperLimit, int divideBy) {
    return createListOfEmployees(upperLimit).parallelStream()
            .filter(Employee -&gt; Employee.getNumber() % divideBy == 0).collect(Collectors.toList());

}


Note that the constructor is declared private since it is useless in this context. Also, addListOfEmployees is not the best possible name; I used createListOfEmployees instead.

Also, since you are printing employees, it would be very nice if they have overriden the toString method.

All in all, I had this in mind:

Employee.java:

public class Employee {

    private final long number;

    public Employee() {
        this(0L);
    }

    public Employee(long number) {
        this.number = number;
    }

    public long getNumber() {
        return number;
    }

    public String toString() {
        return "[Employee ID: " + number + "]";
    }
}


AddEmployees.java:

import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class AddEmployees extends Employee {

    private AddEmployees() {}

    public static List&lt;Employee&gt; createListOfEmployees(long upperLimit) {
        return LongStream.range(0, upperLimit)
                         .mapToObj(Employee::new)
                         .collect(Collectors.toList());
    }

    public static List&lt;Employee&gt; getAllNumbersDividedByProvidedNumber(long upperLimit, int divideBy) {
        return createListOfEmployees(upperLimit).parallelStream()
                .filter(Employee -&gt; Employee.getNumber() % divideBy == 0).collect(Collectors.toList());

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of employees: ");
        int numberOfEmployees = sc.nextInt();
        System.out.print("Enter the divisor: ");
        int divisor = sc.nextInt();

        getAllNumbersDividedByProvidedNumber(numberOfEmployees, 
                                             divisor)
                .forEach(System.out::println);
    }
}


Additional advice

You can shorten your getEmployeeDividedBy a bit:

public static List&lt;Employee&gt; getEmployeeDividedBy(long upperLimit, long dividedby) {
    return LongStream.range(0, upperLimit)
                     .filter(l -&gt; l % dividedby == 0)
                     .mapToObj(Employee::new)
                     .collect(Collectors.toList());
}


Note that I removed .collect(Collectors.toList()).stream() since it is not required. Also, I first filter, and map only after that since this prunes away some pointless computation.

Hope that helps.