class Checker{
void CheckingNameFromDeptCode(LinkedList<Employee1> empObj, String deptID)
{
for (int i = 0; i < empObj.size(); i++) {
if (empObj.get(i).getDeptID().equals(deptID)) {
System.out.println(empObj.get(i).getEmpName());
}
}
}
}
This is the method I created to check for the employees for the inputed dept list. But I been told to use streams/lambda in java 8 for the iteration instead of the good old for loop I use.
Below is the main method.
import java.util.*;
public class Manager{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Employee1> employeeObj = new LinkedList<Employee1>();
Checker empBusObj = new Checker();
employeeObj.add(new Employee1("Souvik", "D1", "Development", "L1", "Kolkata"));
employeeObj.add(new Employee1("Anirban", "D2", "HR", "L2", "Bangalore"));
employeeObj.add(new Employee1("Joydeep", "D3", "Design", "L3", "Delhi"));
employeeObj.add(new Employee1("Rakesh", "D2", "HR", "L4", "Pune"));
System.out.print("Enter the choices : ");
int ch = sc.nextInt();
String deptInput;
String locInput;
switch (ch)
{
case 1:
System.out.print("Enter the department code : ");
deptInput = sc.next();
deptInput = deptInput.toUpperCase();
empBusObj.CheckingNameFromDeptCode(employeeObj, deptInput);
break;
forloop") instead of iterating by index. Not everything needs to use streams just because.LinkedListand callingget(int)on it in a loop, even twice in a row, is very inefficient and counteracting any benefit you might have thought you gained from usingLinkedListinstead ofArrayList. As elaborated in “When to use LinkedList over ArrayList in Java?”, you will almost never get a benefit fromLinkedListanyway.List<Employee1>as parameter, to not mandate a particular implementation and usefor(Employee1 e: empObj) if(e.getDeptID().equals(deptID)) System.out.println(e.getEmpName());as suggested by kaya3, as this loop is not only easier to read, it avoids the problems of using random access operations on aLinkedList. The caller may still switch toArrayListthen, for efficiency, without a change to this method.