From 6ddf37aefc559e5d5c8126552523499ffd9ca56b Mon Sep 17 00:00:00 2001 From: Ismail cisse Date: Sat, 21 Sep 2024 18:48:51 +0200 Subject: [PATCH] updated code to make it less verbose and removed unecessary variables --- src/main/java/Main.java | 155 ++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 95 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 843c2bd..1404697 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,102 +1,67 @@ -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; public class Main { - public static void main(String[] args) { - List people = getPeople(); - - // Imperative approach ❌ - - /* - - List females = new ArrayList<>(); - - for (Person person : people) { - - if (person.getGender().equals(Gender.FEMALE)) { - females.add(person); - } + public static void main(String[] args) { + List people = getPeople(); + + // Filter females and print immediately (combined filtering and printing) + people.stream() + .filter(p -> p.getGender() == Gender.FEMALE) // Used '==' for enums (more idiomatic) + .forEach(System.out::println); // Reduced lines by combining filtering and printing + + // Sort by age and gender in reversed order and print immediately (combined sorting and printing) + people.stream() + .sorted(Comparator.comparing(Person::getAge) + .thenComparing(Person::getGender).reversed()) // Sorting done inline + .forEach(System.out::println); // Combined sorting and printing, saving extra steps + + // All match (prints result directly instead of storing in a variable) + System.out.println(people.stream().allMatch(p -> p.getAge() > 8)); // Removed intermediate variable + + // Any match (prints result directly instead of storing in a variable) + System.out.println(people.stream().anyMatch(p -> p.getAge() > 121)); // Reduced verbosity + + // None match (prints result directly instead of storing in a variable) + System.out.println(people.stream().noneMatch(p -> p.getName().equals("Antonio"))); // Same as above + + // Max (prints the max person by age immediately if present) + people.stream() + .max(Comparator.comparing(Person::getAge)) + .ifPresent(System.out::println); // Combined max operation with print + + // Min (prints the min person by age immediately if present) + people.stream() + .min(Comparator.comparing(Person::getAge)) + .ifPresent(System.out::println); // Combined min operation with print + + // Group by gender and print the groups inline (combined grouping and printing) + people.stream() + .collect(Collectors.groupingBy(Person::getGender)) + .forEach((gender, persons) -> { // Removed unnecessary variable assignment + System.out.println(gender); + persons.forEach(System.out::println); // Prints each person within the group + }); + + // Find the oldest female and print her name (combined filtering, max operation, and printing) + people.stream() + .filter(p -> p.getGender() == Gender.FEMALE) // Filtering for females + .max(Comparator.comparing(Person::getAge)) // Find the oldest + .map(Person::getName) // Get the name + .ifPresent(System.out::println); // Print if present (all in one chain) } - females.forEach(System.out::println); - - */ - - // Declarative approach ✅ - - // Filter - List females = people.stream() - .filter(person -> person.getGender().equals(Gender.FEMALE)) - .collect(Collectors.toList()); - -// females.forEach(System.out::println); - - // Sort - List sorted = people.stream() - .sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getGender).reversed()) - .collect(Collectors.toList()); - -// sorted.forEach(System.out::println); - - // All match - boolean allMatch = people.stream() - .allMatch(person -> person.getAge() > 8); - -// System.out.println(allMatch); - // Any match - boolean anyMatch = people.stream() - .anyMatch(person -> person.getAge() > 121); - -// System.out.println(anyMatch); - // None match - boolean noneMatch = people.stream() - .noneMatch(person -> person.getName().equals("Antonio")); - -// System.out.println(noneMatch); - - // Max - people.stream() - .max(Comparator.comparing(Person::getAge)); -// .ifPresent(System.out::println); - - // Min - people.stream() - .min(Comparator.comparing(Person::getAge)); -// .ifPresent(System.out::println); - - // Group - Map> groupByGender = people.stream() - .collect(Collectors.groupingBy(Person::getGender)); - -// groupByGender.forEach((gender, people1) -> { -// System.out.println(gender); -// people1.forEach(System.out::println); -// System.out.println(); -// }); - - Optional oldestFemaleAge = people.stream() - .filter(person -> person.getGender().equals(Gender.FEMALE)) - .max(Comparator.comparing(Person::getAge)) - .map(Person::getName); - - oldestFemaleAge.ifPresent(System.out::println); - } - - private static List getPeople() { - return List.of( - new Person("Antonio", 20, Gender.MALE), - new Person("Alina Smith", 33, Gender.FEMALE), - new Person("Helen White", 57, Gender.FEMALE), - new Person("Alex Boz", 14, Gender.MALE), - new Person("Jamie Goa", 99, Gender.MALE), - new Person("Anna Cook", 7, Gender.FEMALE), - new Person("Zelda Brown", 120, Gender.FEMALE) - ); - } - + // Mock data for testing + private static List getPeople() { + return List.of( + new Person("Antonio", 20, Gender.MALE), + new Person("Alina Smith", 33, Gender.FEMALE), + new Person("Helen White", 57, Gender.FEMALE), + new Person("Alex Boz", 14, Gender.MALE), + new Person("Jamie Goa", 99, Gender.MALE), + new Person("Anna Cook", 7, Gender.FEMALE), + new Person("Zelda Brown", 120, Gender.FEMALE) + ); + } }