Skip to content

Commit

Permalink
Update Person class and add parallel stream in UsePerson
Browse files Browse the repository at this point in the history
Updated code in Person.java by adding comments on records and copy constructor. In UsePerson.java, additional code for sequential and parallel streams with maps and peeks were added, enhancing code readability and understanding of process threads. Minor tweaks were also made in the LinkedList's Supplier, BiConsumer syntax.
  • Loading branch information
kousen committed Feb 6, 2024
1 parent 02e0a13 commit fd28f25
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

import java.util.Objects;

// Records:
// - became GA in Java 16
// - immutable data holders
// - constructor goes BEFORE the braces
// - autogenerate toString(), equals(), hashCode() methods
// - "getter" is a method that matches the name of the field, as in "name()"

// public record Person(String name) {
// public Person() {
// this("Unknown");
// }
//
// public Person(String... names) {
// this(String.join(" ", names));
// }
// }


// POJO: Plain Old Java Object
public class Person {
private String name;

Expand All @@ -11,6 +30,7 @@ public Person(String name) {
this.name = name;
}

// copy constructor
public Person(Person other) {
this.name = other.name;
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ public static void main(String[] args) {
.collect(Collectors.toList()); // Converts Stream<Person> to List<Person>
System.out.println(people);

// Sequential stream
people = names.stream()
.map(Person::new) // uses the Person(String) ctr
.peek(person -> System.out.println(person + " processed by " +
Thread.currentThread()
.getName()))
// .map(Person::new) // uses the Person(Person) ctr
.collect(Collectors.toList());
System.out.println(people);

// Parallel stream
people = names.parallelStream()
.map(Person::new) // uses the Person(String) ctr
.peek(person -> System.out.println(person + " processed by " +
Thread.currentThread().getName()))
.collect(Collectors.toList());
System.out.println(people);

Person[] peopleArray = names.stream()
.map(Person::new)
.toArray(Person[]::new);
//.toArray(value -> new Person[value]);
//.toArray(value -> new Person[value]);
System.out.println(Arrays.toString(peopleArray));

List<String> fullNames = Arrays.asList(
Expand All @@ -49,7 +61,7 @@ public static void main(String[] args) {
LinkedList<Person> linkedPersons = names.stream()
.map(Person::new)
.collect(
() -> new LinkedList<Person>(), // Supplier<LinkedList>
() -> new LinkedList<>(), // Supplier<LinkedList>
(list, person) -> list.add(person), // BiConsumer<LinkedList, Person>
(list1, list2) -> list1.addAll(list2)); // BiConsumer<LinkedList, LinkedList>
System.out.println(linkedPersons);
Expand Down

0 comments on commit fd28f25

Please sign in to comment.