ChatGPT에게 실생활 시나리오를 포함하여 Java Streams API를 가르쳐 달라고 부탁했습니다. 이 글에서 ChatGPT가 얼마나 잘 해냈는지 보실래요?
기본 직원 클래스 만들기
Prompt 1: create an employee class with multiple data fields, make sure to include a list, a map, dates and numbers along with Boolean and string.
(list, map, dates와 numbers, boolean, string을 포함한 여러 데이터 필드로 직원 클래스를 만들어 줘)
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Employee {
private String name;
private int age;
private double salary;
private List<String> skills;
private Map<String, String> contactInfo;
private Date hireDate;
private boolean isActive;
// Constructors
public Employee() {
}
public Employee(String name, int age, double salary, List<String> skills, Map<String, String> contactInfo,
Date hireDate, boolean isActive) {
this.name = name;
this.age = age;
this.salary = salary;
this.skills = skills;
this.contactInfo = contactInfo;
this.hireDate = hireDate;
this.isActive = isActive;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
public Map<String, String> getContactInfo() {
return contactInfo;
}
public void setContactInfo(Map<String, String> contactInfo) {
this.contactInfo = contactInfo;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean active) {
isActive = active;
}
// toString() method to display the employee details
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", skills=" + skills +
", contactInfo=" + contactInfo +
", hireDate=" + hireDate +
", isActive=" + isActive +
'}';
}
}
직원 만들기
Prompt 2: create some employees for the above class, 15 would be good.
(위 클래스를 사용해서 직원을 만들어줘. 15명 정도가 좋을 것 같아.)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class EmployeeDemo {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
// Create date format
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
// Create Employee objects
Employee emp1 = new Employee("John Doe", 30, 50000.0,
Arrays.asList("Java", "Python", "SQL"),
getContactInfo("john.doe@example.com", "1234567890"),
dateFormat.parse("01/04/2020"), true);
employees.add(emp1);
Employee emp2 = new Employee("Alice Smith", 34, 60000.0,
Arrays.asList("JavaScript", "HTML", "CSS"),
getContactInfo("alice.smith@example.com", "9876543210"),
dateFormat.parse("12/01/2018"), true);
employees.add(emp2);
Employee emp3 = new Employee("Robert Johnson", 28, 45000.0,
Arrays.asList("C++", "Python", "Data Analysis"),
getContactInfo("robert.johnson@example.com", "4567890123"),
dateFormat.parse("15/07/2019"), false);
employees.add(emp3);
Employee emp4 = new Employee("Emily Brown", 32, 55000.0,
Arrays.asList("Java", "JavaScript", "Angular"),
getContactInfo("emily.brown@example.com", "7890123456"),
dateFormat.parse("05/03/2017"), true);
employees.add(emp4);
Employee emp5 = new Employee("Michael Wilson", 29, 52000.0,
Arrays.asList("Python", "Machine Learning", "Big Data"),
getContactInfo("michael.wilson@example.com", "2345678901"),
dateFormat.parse("20/11/2018"), true);
employees.add(emp5);
Employee emp6 = new Employee("Olivia Davis", 31, 48000.0,
Arrays.asList("HTML", "CSS", "UI/UX Design"),
getContactInfo("olivia.davis@example.com", "6789012345"),
dateFormat.parse("02/09/2019"), true);
employees.add(emp6);
Employee emp7 = new Employee("James Miller", 33, 58000.0,
Arrays.asList("Java", "Spring Framework", "Hibernate"),
getContactInfo("james.miller@example.com", "8901234567"),
dateFormat.parse("14/06/2020"), true);
employees.add(emp7);
Employee emp8 = new Employee("Sophia Wilson", 27, 43000.0,
Arrays.asList("Python", "Data Science", "Data Visualization"),
getContactInfo("sophia.wilson@example.com", "4567890123"),
dateFormat.parse("23/02/2021"), true);
employees.add(emp8);
Employee emp9 = new Employee("Liam Anderson", 35, 62000.0,
Arrays.asList("Java", "Spring Boot", "Microservices"),
getContactInfo("liam.anderson@example.com", "6789012345"),
dateFormat.parse("17/10/2017"), false);
employees.add(emp9);
Employee emp10 = new Employee("Ava Lee", 28, 51000.0,
Arrays.asList("JavaScript", "React", "Node.js"),
getContactInfo("ava.lee@example.com", "9012345678"),
dateFormat.parse("29/12/2018"), true);
employees.add(emp10);
Employee emp11 = new Employee("Mason Harris", 30, 54000.0,
Arrays.asList("Python", "Machine Learning", "Natural Language Processing"),
getContactInfo("mason.harris@example.com", "3456789012"),
dateFormat.parse("16/08/2019"), true);
employees.add(emp11);
Employee emp12 = new Employee("Isabella Martinez", 29, 47000.0,
Arrays.asList("HTML", "CSS", "Bootstrap"),
getContactInfo("isabella.martinez@example.com", "7890123456"),
dateFormat.parse("25/05/2020"), true);
employees.add(emp12);
Employee emp13 = new Employee("William Wilson", 31, 60000.0,
Arrays.asList("Java", "Spring MVC", "RESTful APIs"),
getContactInfo("william.wilson@example.com", "1234567890"),
dateFormat.parse("03/05/2017"), true);
employees.add(emp13);
Employee emp14 = new Employee("Charlotte Davis", 32, 52000.0,
Arrays.asList("Python", "Data Analysis", "SQL"),
getContactInfo("charlotte.davis@example.com", "5678901234"),
dateFormat.parse("22/11/2018"), false);
employees.add(emp14);
Employee emp15 = new Employee("Benjamin Taylor", 34, 58000.0,
Arrays.asList("JavaScript", "React Native", "Firebase"),
getContactInfo("benjamin.taylor@example.com", "9012345678"),
dateFormat.parse("11/07/2019"), true);
employees.add(emp15);
// Display employee details
for (Employee emp : employees) {
System.out.println(emp);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
// Helper method to create contact info map
private static Map<String, String> getContactInfo(String email, String phone) {
Map<String, String> contactInfo = new HashMap<>();
contactInfo.put("Email", email);
contactInfo.put("Phone", phone);
return contactInfo;
}
}
좋은 부분!
Prompt 3: now I want to learn java streams API for advanced java developement, please give me some real life complex problems with solution for above class and employees that you created.
(이제 Java Streams API를 배우고 싶은데, 위에서 만든 클래스로 실생활의 복잡한 문제를 다뤄줘.)
// Problem: Get a list of active employees with a salary greater than 50000
List<Employee> activeEmployeesWithHighSalary = employees.stream()
.filter(Employee::isActive)
.filter(emp -> emp.getSalary() > 50000)
.collect(Collectors.toList());
System.out.println(activeEmployeesWithHighSalary);
// Problem: Get the total number of employees who possess Java and Python skills
long count = employees.stream()
.filter(emp -> emp.getSkills().contains("Java") && emp.getSkills().contains("Python"))
.count();
System.out.println("Number of employees with Java and Python skills: " + count);
// Problem: Find the employee with the highest salary
Optional<Employee> employeeWithHighestSalary = employees.stream()
.max(Comparator.comparingDouble(Employee::getSalary));
employeeWithHighestSalary.ifPresent(emp -> System.out.println("Employee with highest salary: " + emp));
// Problem: Group employees by their age
Map<Integer, List<Employee>> employeesByAge = employees.stream()
.collect(Collectors.groupingBy(Employee::getAge));
employeesByAge.forEach((age, empList) -> System.out.println("Employees with age " + age + ": " + empList));
// Problem: Calculate the average salary of all employees
double averageSalary = employees.stream()
.mapToDouble(Employee::getSalary)
.average()
.orElse(0.0);
System.out.println("Average salary: " + averageSalary);
ChatGPT는 필터링, 비교, 그룹화와 같은 몇 가지 기본 개념을 포함하기에 충분한 예제들을 제시했지만, 여전히 많은 것들을 놓치고 있었으며 문제가 전혀 복잡하지 않았습니다. 그래서 ChatGPT에게 더 복잡한 문제를 알려달라고 요청했습니다.
Prompt 4: give me more complex examples.
(더 복잡한 예시들을 알려줘.)
// Problem: Find the employee with the most number of skills
Optional<Employee> mostSkilledEmployee = employees.stream()
.max(Comparator.comparingInt(emp -> emp.getSkills().size()));
mostSkilledEmployee.ifPresent(emp -> System.out.println("Employee with the most skills: " + emp));
// Problem: Calculate the total salary of active employees in each department
Map<String, Double> departmentSalarySum = employees.stream()
.filter(Employee::isActive)
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingDouble(Employee::getSalary)));
departmentSalarySum.forEach((department, totalSalary) ->
System.out.println("Total salary in " + department + ": " + totalSalary));
// Problem: Find the employee who joined first in each department
Map<String, Optional<Employee>> earliestEmployeeByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.minBy(Comparator.comparing(Employee::getHireDate))));
earliestEmployeeByDept.forEach((department, employee) ->
employee.ifPresent(emp -> System.out.println("Earliest joined employee in " + department + ": " + emp)));
// Problem: Check if any employee has both a skill and a contact detail containing the keyword "example"
boolean hasEmployeeWithSkillAndKeyword = employees.stream()
.anyMatch(emp -> emp.getSkills().stream().anyMatch(skill ->
emp.getContactInfo().values().stream().anyMatch(contact ->
contact.contains("example"))));
System.out.println("Has employee with skill and keyword: " + hasEmployeeWithSkillAndKeyword);
// Problem: Sort employees based on their skills (lexicographically) and then by salary (descending)
List<Employee> sortedEmployees = employees.stream()
.sorted(Comparator.comparing(Employee::getSkills, Comparator.comparing(List::toString))
.thenComparingDouble(Employee::getSalary).reversed())
.collect(Collectors.toList());
sortedEmployees.forEach(System.out::println);
이번에는 예제가 조금 나아졌지만, 여전히 Streams API의 많은 메서드를 놓쳤습니다. 그래서 다른 명령을 실행했습니다.
Prompt 5: I think you have not covered many methods in the API and also missed on some real complex cases, please bring out everything you have.
(많은 메서드를 다루지 못하였고 실제 복잡한 케이스도 놓친 것 같은데, 가지고 있는 모든 것을 총 동원해줘)
// Problem: Calculate the average age of employees in each department
Map<String, Double> departmentAverageAge = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.averagingInt(Employee::getAge)));
departmentAverageAge.forEach((department, averageAge) ->
System.out.println("Average age in " + department + ": " + averageAge));
// Problem: Find employees who have duplicate skills
Map<List<String>, List<Employee>> employeesWithDuplicateSkills = employees.stream()
.collect(Collectors.groupingBy(Employee::getSkills));
employeesWithDuplicateSkills.values().stream()
.filter(empList -> empList.size() > 1)
.flatMap(List::stream)
.distinct()
.forEach(System.out::println);
// Problem: Calculate the total salary of employees for each year since their hire date
Map<Integer, Double> yearlySalarySum = employees.stream()
.collect(Collectors.groupingBy(emp -> getYear(emp.getHireDate()),
Collectors.summingDouble(Employee::getSalary)));
yearlySalarySum.forEach((year, totalSalary) ->
System.out.println("Total salary in year " + year + ": " + totalSalary));
// Problem: Split employees into two groups based on their salary (above and below average)
double averageSalary = employees.stream()
.mapToDouble(Employee::getSalary)
.average()
.orElse(0.0);
Map<Boolean, List<Employee>> employeesBySalaryGroup = employees.stream()
.collect(Collectors.partitioningBy(emp -> emp.getSalary() > averageSalary));
List<Employee> aboveAverageSalary = employeesBySalaryGroup.get(true);
List<Employee> belowAverageSalary = employeesBySalaryGroup.get(false);
System.out.println("Employees with salary above average: " + aboveAverageSalary);
System.out.println("Employees with salary below average: " + belowAverageSalary);
// Problem: Increase the salary of employees with active status by 10% and decrease the salary of inactive employees by 5%
List<Employee> updatedEmployees = employees.stream()
.map(emp -> {
double newSalary = emp.isActive()
? emp.getSalary() * 1.10
: emp.getSalary() * 0.95;
emp.setSalary(newSalary);
return emp;
})
.collect(Collectors.toList());
updatedEmployees.forEach(System.out::println);
// Problem: Calculate the cumulative years of experience for all employees
int totalYearsOfExperience = employees.stream()
.mapToInt(emp -> getYear(new Date()) - getYear(emp.getHireDate()))
.sum();
System.out.println("Total years of experience: " + totalYearsOfExperience);
// Problem: Calculate the cumulative years of experience for all employees
int totalYearsOfExperience = employees.stream()
.mapToInt(emp -> getYear(new Date()) - getYear(emp.getHireDate()))
.sum();
System.out.println("Total years of experience: " + totalYearsOfExperience);
// Problem: Find the department with the highest average salary
Optional<Map.Entry<String, Double>> departmentWithHighestAvgSalary = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)))
.entrySet()
.stream()
.max(Map.Entry.comparingByValue());
departmentWithHighestAvgSalary.ifPresent(department ->
System.out.println("Department with highest average salary: " + department.getKey()));
// Problem: Get the top three employees with the most diverse (unique) skills
List<Employee> topThreeDiverseEmployees = employees.stream()
.sorted(Comparator.comparingInt(emp -> emp.getSkills().size()))
.limit(3)
.collect(Collectors.toList());
System.out.println("Top three employees with the most diverse skills: " + topThreeDiverseEmployees);
// Problem: Calculate the accumulated total salary of employees in each department over time
Map<String, List<Double>> departmentAccumulatedSalary = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.mapping(Employee::getSalary,
Collectors.collectingAndThen(Collectors.toList(), salaryList -> {
double sum = 0;
List<Double> accumulated = new ArrayList<>();
for (Double salary : salaryList) {
sum += salary;
accumulated.add(sum);
}
return accumulated;
}))));
departmentAccumulatedSalary.forEach((department, accumulatedSalaryList) ->
System.out.println("Accumulated salary in " + department + ": " + accumulatedSalaryList));
// Problem: Find the employee with the most number of skills in each department
Map<String, Optional<Employee>> employeeWithMostSkillsByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.maxBy(Comparator.comparingInt(emp -> emp.getSkills().size()))));
employeeWithMostSkillsByDept.forEach((department, employee) ->
employee.ifPresent(emp -> System.out.println("Employee with the most skills in " + department + ": " + emp)));
// Problem: Partition employees into two groups based on their total years of experience (over and under 5 years)
Map<Boolean, List<Employee>> employeesByExperience = employees.stream()
.collect(Collectors.partitioningBy(emp -> getTotalYearsOfExperience(emp) >= 5));
List<Employee> experiencedEmployees = employeesByExperience.get(true);
List<Employee> inexperiencedEmployees = employeesByExperience.get(false);
System.out.println("Experienced employees (5+ years of experience): " + experiencedEmployees);
System.out.println("Inexperienced employees (less than 5 years of experience): " + inexperiencedEmployees);
이번에는 ChatGPT가 해냈습니다. 우리가 원하는 걸 얻었어요.
ChatGPT를 사용해 기술을 효율적으로 가르칠 수 있습니다. 저는 개념을 알고 있었기 때문에 예시를 요청했습니다. 여기 당신이 몇 가지 시도해 볼 수 있는 것들이 있습니다.
- 중첩 스트림에 대한 몇 가지 예시를 제시하고 각각의 예를 설명해줘
- 부서(department)와 같은 다른 클래스를 만들고 int, double, boolean, date, string을 포함해 줘.
- 부서원과 부서 클래스를 사용해 문제를 만들어 줘
- 문제를 더 복잡하게 만들어 줘
이제 ChatGPT로 학습을 시작해보세요!
출처: https://medium.com/stackademic/asking-chatgpt-to-teach-me-java-streams-api-763492b6da71
'레퍼런스' 카테고리의 다른 글
[Medium] 2024 버클리즈 Java SpringBoot 인터뷰 질문 (1) (0) | 2024.04.01 |
---|---|
[Medium] Kafka vs. RabbitMQ: 알맞은 메시지 브로커 찾기 (0) | 2024.03.25 |
[Medium] 시스템 디자인 인터뷰의 골든룰 (0) | 2024.03.13 |
[Medium] 클린 코드: Null 반환을 피하는 법 (0) | 2024.03.11 |
[Medium] 2024년 자바 개발자 면접 가이드 (2) (0) | 2024.03.04 |