🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this tutorial, we'll develop a Java program to group employees by their city. This technique is widely useful in organizing and analyzing employee data by geographic distribution, which can assist in various strategic decisions.
2. Program Steps
1. Define the Employee class with appropriate attributes such as id, name, age, salary, gender, deptName, city, and constructor.
2. Create a GroupEmployees class that will contain the main method.
3. In the main method, instantiate a list of Employee objects and group them by city.
4. Display the results of the grouping.
3. Code Program
Creating Employee Class
// File: Employee.java
public class Employee {
private int id;
private String name;
private int age;
long salary;
private String gender;
private String deptName;
private String city;
public Employee(int id, String name, int age, long salary, String gender, String deptName, String city) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.gender = gender;
this.deptName = deptName;
this.city = city;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return String.format("%d %s %d %d %s %s %s", id, name, age, salary, gender, deptName, city);
}
}
Create GroupEmployees Class and Write Logic
// File: GroupEmployees.java
import java.util.*;
import java.util.stream.Collectors;
public class GroupEmployees {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai"),
new Employee(2, "Rahul", 34, 130000, "M", "Tech", "Bangalore"),
new Employee(3, "Vishal", 28, 110000, "M", "Tech", "Mumbai"),
new Employee(4, "Lakshmi", 45, 150000, "F", "HR", "Bangalore")
);
Map<String, List<Employee>> groupedByCity = employees.stream()
.collect(Collectors.groupingBy(Employee::getCity));
groupedByCity.forEach((city, empList) -> {
System.out.println("City: " + city);
empList.forEach(System.out::println);
});
}
}
// File: GroupEmployees.java
import java.util.*;
import java.util.stream.Collectors;
public class GroupEmployees {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai"),
new Employee(2, "Rahul", 34, 130000, "M", "Tech", "Bangalore"),
new Employee(3, "Vishal", 28, 110000, "M", "Tech", "Mumbai"),
new Employee(4, "Lakshmi", 45, 150000, "F", "HR", "Bangalore")
);
Map<String, List<Employee>> groupedByCity = employees.stream()
.collect(Collectors.groupingBy(Employee::getCity));
groupedByCity.forEach((city, empList) -> {
System.out.println("City: " + city);
empList.forEach(System.out::println);
});
}
}Output:
City: Mumbai 1 Aditi 30 100000 F HR Mumbai 3 Vishal 28 110000 M Tech Mumbai City: Bangalore 2 Rahul 34 130000 M Tech Bangalore 4 Lakshmi 45 150000 F HR Bangalore
Explanation:
1. The Employee class is defined with private fields and a constructor to initialize the object.
2. The GroupEmployees class contains the main method where the list of employees is created.
3. Employees are grouped by city using the collectors.groupingBy method.
4. Each city and its corresponding list of employees are printed on the console.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment