Skip to content

Commit

Permalink
Add Record class and FileFilter Tests; Update UsePerson
Browse files Browse the repository at this point in the history
Introduced a Java record, PersonRecord, in the Person.java file to provide an immutable data holder functionality. Inclusion of 'record class' also brings auto-generation of equals(), hashCode() and toString() methods. Further, added FileFilter Tests in FileFilterTest.java, ensuring that different filtering scenarios work as expected. Modifications were also made to UsePerson.java file to improve code readability and performance via using parallel streams and the addition of useful comments for better understanding of the code.
  • Loading branch information
kousen committed Nov 30, 2023
1 parent b756cd7 commit abe8cd5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import java.util.Objects;

// Records:
// - are immutable data holders
// - have "canonical" or "primary" constructors BEFORE the braces
// - autogenerate equals(), hashCode(), and toString()
// - have "getters" whose names match the field names, e.g., name()
record PersonRecord(String name) {}

public class Person {
private String name;

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.Collectors;

public class UsePerson {
@SuppressWarnings("Convert2MethodRef")
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");

Expand All @@ -17,21 +18,23 @@ public static void main(String[] args) {
}
System.out.println(beatles);

// Functional approach:
List<Person> people = names.stream() // Stream<String>
.map(name -> new Person(name)) // Stream<Person>
.collect(Collectors.toList()); // Converts Stream<Person> to List<Person>
System.out.println(people);

people = names.stream()
.map(Person::new) // uses the Person(String) ctr
.map(Person::new) // uses the Person(String) ctr --> Stream<Person>
// .map(Person::new) // uses the Person(Person) ctr
.collect(Collectors.toList());
System.out.println(people);

Person[] peopleArray = names.stream()
.map(Person::new)
.toArray(Person[]::new);
//.toArray(value -> new Person[value]);
.toArray(Person[]::new); // dimension an array of Person whose size
// is the same as the number of elements in the stream
//.toArray(value -> new Person[value]); // lambda equivalent of the constructor reference
System.out.println(Arrays.toString(peopleArray));

List<String> fullNames = Arrays.asList(
Expand All @@ -44,27 +47,33 @@ public static void main(String[] args) {
System.out.println(people.getClass().getName());

// p1..p5 | p6..p10 | p11..p15 | p16..p20 // say you have 4 cores and run in parallel
// l1 l2 l3 l4
// l1 l2 l3 l4 // Java uses a ForkJoinPool by default
// list
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);

linkedPersons = names.stream()
linkedPersons = names.parallelStream()
.map(Person::new)
.collect(
LinkedList::new, // Supplier<LinkedList>
LinkedList::add, // BiConsumer<LinkedList, Person>
LinkedList::addAll); // BiConsumer<LinkedList, LinkedList>
System.out.println(linkedPersons);

linkedPersons = names.stream()
linkedPersons = names.parallelStream()
.map(Person::new)
.collect(Collectors.toCollection(LinkedList::new));
.collect(Collectors.toCollection(LinkedList::new)); // collect preserves order
System.out.println(linkedPersons);

List<Person> persons = new ArrayList<>();
names.parallelStream()
.map(Person::new)
.forEach(persons::add); // forEach does not preserve order
System.out.println(persons);
}
}
81 changes: 81 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class FileFilterTest {

private final File root = new File("src/main/java");

@Test
void listFiles() {
File[] files = root.listFiles();
assert files != null;
assertEquals(22, files.length);
}

@SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"})
@Test
void listDirectories_anonInnerClass() {
File[] directories = root.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
assert directories != null;
assertEquals(14, directories.length);
}

@SuppressWarnings("Convert2MethodRef")
@Test
void listDirectories_expressionLambda() {
File[] directories = root.listFiles(pathname -> pathname.isDirectory());
assert directories != null;
assertEquals(14, directories.length);
}

@Test
void listDirectories_methodReference() {
File[] directories = root.listFiles(File::isDirectory);
assert directories != null;
assertEquals(14, directories.length);
}

@Test
void listDirectories_blockLambda() {
File[] directories = root.listFiles(pathname -> {
System.out.println("Checking " + pathname);
return pathname.isDirectory();
});
assert directories != null;
assertEquals(14, directories.length);
}

@Test
void listJavaSrcFiles_fileFilter() {
File[] javaSrcFiles = root.listFiles(pathname -> pathname.getName().endsWith(".java"));
assert javaSrcFiles != null;
assertEquals(8, javaSrcFiles.length);
}

@Test
void listJavaSrcFiles_fileNameFilter() {
File[] javaSrcFiles = root.listFiles((dir, name) -> name.endsWith(".java"));
assert javaSrcFiles != null;
assertEquals(8, javaSrcFiles.length);
}

@Test
void listJavaSrcFiles_assignToVariable() {
FilenameFilter filter = (File dir, String name) -> name.endsWith(".java");
File[] javaSrcFiles = root.listFiles(filter);
assert javaSrcFiles != null;
assertEquals(8, javaSrcFiles.length);
}
}

0 comments on commit abe8cd5

Please sign in to comment.