using java 8 stream to process data in java
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));
Also in Java:
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- how to make a new arraylist java
- Category
- Java
- Title
- how to byheart faster
- Category
- Java
- Title
- how to get binary value in java
- Category
- Java
- Title
- java arraylist to string
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- android hide and show bottom navigation
- Category
- Java
- Title
- java int to binary
- Category
- Java
- Title
- date format in java
- Category
- Java
- Title
- how to import jframe in java
- Category
- Java
- Title
- java string indexof
- Category
- Java
- Title
- java string util if empty default
- Category
- Java
- Title
- declare hashmap java
- Category
- Java
- Title
- Jlabel icon
- Category
- Java
- Title
- jstl core tags
- Category
- Java
- Title
- how to print to console in java
- Category
- Java
- Title
- spigot custom mob
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- convert integer array to string array
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- what is java
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- arraylist with values java
- Category
- Java
- Title
- java beginners book
- Category
- Java
- Title
- java implement interface
- Category
- Java
- Title
- java insert array
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- get raondom from array java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- How do you count characters in a string array in Java?
- Category
- Java
- Title
- filter in list by time java
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- how to extract data from delimited text file in java
- Category
- Java
- Title
- jackson ignore values if empty
- Category
- Java
- Title
- Java loop throug gson JsonElement
- Category
- Java
- Title
- how to convert a jsonobject to a dbobject
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- java for increment by 2
- Category
- Java
- Title
- java dictionary
- Category
- Java
- Title
- java iterate over list
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- javafx datepicker
- Category
- Java
- Title
- java iterate over a string
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- how to set frame colo in java
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- convert array of int to arraylist java
- Category
- Java
- Title
- java get location of jar file
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- how to quicksort a string array in java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- how to get witdth of window android
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- int to binary string java
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- java string to lower case
- Category
- Java
- Title
- how to declare a linked list in java
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- java decler variabel
- Category
- Java
- Title
- java convert string with commas to long
- Category
- Java
- Title
- double parse jtextfield
- Category
- Java
- Title
- foreach not applicable to type - binary tree sort
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- addition of two numbers in java
- Category
- Java
- Title
- pascals triangle java
- Category
- Java
- Title
- java string replace character at position
- Category
- Java
- Title
- change brightness of image in java
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- binary to octal conversion java program
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- bukkit register commands
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- Building a maven EAR project and specifying the configuration of which projects to include, what is the element in the plugin configuration that contains Enterprise Java Bean Projects:
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- add one character to string java
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- how to end a program in an if statement java
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- java round up
- Category
- Java
- Title
- write file java
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- get last element of array java
- Category
- Java
- Title
- string replace java
- Category
- Java