Printing Names of All Departments from Employee Class using Java
Categories: Java 8(JDK1.8) Java Java Examples
Printing Names of All Departments from Employee Class using Java
package r4r.co.in;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class EmployeePrintingNamesOfAllDepartments {
public static void main(String[] args) {
//Printing Names of All Departments
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Rajesh", 121.0, "IT","Lucknow",24,"Male"));
list.add(new Employee("Rani", 122.0, "ACT","NOIDA",35,"Female"));
list.add(new Employee("Rahul", 122.0, "ACT","NOIDA",35,"Male"));
list.add(new Employee("Rohit", 123.0, "ACT","Delhi",36,"Male"));
list.add(new Employee("Sonu", 124.0, "OPR","Ambedkar Nagar",37,"Male"));
System.out.println("Printing Names of All Departments............");
List<String> allDepartmemts = list.stream().map(Employee::getDep).distinct().collect(Collectors.toList());
System.out.println(allDepartmemts);
}
}
class Employee {
String name;
double sal;
String dep;
String city;
int age;
String gender;
public Employee(String name, double sal, String dep, String city,int age,String gender) {
super();
this.name = name;
this.sal = sal;
this.dep = dep;
this.city = city;
this.age=age;
this.gender=gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [name=" + name + ", sal=" + sal + ", dep=" + dep + ", city=" + city + ", age=" + age + ", gender=" + gender+ "]";
}
}