using java 8 stream to process data in java

public class Employee {

	private int employeeID;
	private String employeeName;
	private String employeeGender;
	private String employeeCountry;
	private String employeeState;
	private String employeeCity;
	
	public Employee() {
		// TODO Auto-generated constructor stub
	}

	public Employee(int employeeID, String employeeName, String employeeGender, String employeeCountry,
			String employeeState, String employeeCity) {
		super();
		this.employeeID = employeeID;
		this.employeeName = employeeName;
		this.employeeGender = employeeGender;
		this.employeeCountry = employeeCountry;
		this.employeeState = employeeState;
		this.employeeCity = employeeCity;
	}

	public int getEmployeeID() {
		return employeeID;
	}

	public void setEmployeeID(int employeeID) {
		this.employeeID = employeeID;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeGender() {
		return employeeGender;
	}

	public void setEmployeeGender(String employeeGender) {
		this.employeeGender = employeeGender;
	}

	public String getEmployeeCountry() {
		return employeeCountry;
	}

	public void setEmployeeCountry(String employeeCountry) {
		this.employeeCountry = employeeCountry;
	}

	public String getEmployeeState() {
		return employeeState;
	}

	public void setEmployeeState(String employeeState) {
		this.employeeState = employeeState;
	}

	public String getEmployeeCity() {
		return employeeCity;
	}

	public void setEmployeeCity(String employeeCity) {
		this.employeeCity = employeeCity;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[Employee ID : " + employeeID + ", Employee Name : " + employeeName + ", Employee Gender : "
				+ employeeGender + ", Employee Country : " + employeeCountry + ", Employee State : " + employeeState
				+ ", Employee City : " + employeeCity + "]";
	}
	
	public static void main(String[] args) {
		ArrayList<Employee> employees=new ArrayList<Employee>();
		
		employees.add(new Employee(101, "John", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(91, "Jacob", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(111, "Lisa", "F", "United States", "California", "Los Angeles"));
		employees.add(new Employee(97, "Mary", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(76, "Christine", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(114, "David", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(103, "Kevin", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(109, "Joe", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(119, "Mathew", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(99, "Angelina", "F", "United States", "California", "San Diego"));
		employees.add(new Employee(98, "Tom", "M", "United States", "California", "San Diego"));
		employees.add(new Employee(116, "Curl", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(66, "Christopher", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(56, "Chelse", "F", "United States", "California", "Oakland"));
		employees.add(new Employee(88, "Murali", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(87, "Daisy", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(85, "Niza", "F", "United States", "Virginia", "Richmond"));
		employees.add(new Employee(86, "Chris", "M", "United States", "Virginia", "Fairfax"));
		employees.add(new Employee(90, "Andrew", "M", "United States", "Virginia", "Reston"));
		
	}

}

Operations:

1. Get list of all the employees from "California"; Return a List
2. Count the number of Females; Return a Count
3. Add 10 to the ID of each Employee; Return the updated List
4. Sort in the Descending order by employee name (z-a); Return the List
5. Get the details of the second highest employee ID; Return the employee

Solution:

System.out.println(employees.stream().filter(employee->employee.getEmployeeState().equals("California")).collect(Collectors.toList()));
System.out.println(employees.stream().map(emp->emp.getEmployeeID()+10).collect(Collectors.toList()));
System.out.println(employees.stream().filter(employee->employee.getEmployeeGender().equalsIgnoreCase("f")).count());
System.out.println(employees.stream().sorted((e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName())).collect(Collectors.toList()));
Collections.sort(employees, (s1,s2)-> s2.getEmployeeID() - s1.getEmployeeID());
System.out.println(employees.get(1));

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source