Skip to content

Commit

Permalink
Refactor and add features to several classes
Browse files Browse the repository at this point in the history
Several code enhancements were implemented, including the removal of an unused Logger object in LazyStreams class, a simplification in the use of isEmpty() in FlatMapDemo, and the adjustment of lambdas to method references in UsePerson. In addition, a new CompanyEmployee class was created, and a previously commented test was uncommented and fixed in the CompanyEmployeeTest class.
  • Loading branch information
kousen committed May 21, 2024
1 parent 005e5ab commit 03b8554
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package interfaces;

public class CompanyEmployee implements Company, Employee {
private final String first;
private final String last;

public CompanyEmployee(String first, String last) {
this.first = first;
this.last = last;
}

public String getName() {
return Employee.super.getName() + " works for " + Company.super.getName();
}

@Override
public String getFirst() {
return first;
}

@Override
public String getLast() {
return last;
}

@Override
public void doWork() {
System.out.println("Converting caffeine into code for $$$");
}
}
16 changes: 11 additions & 5 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package lambdas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

@SuppressWarnings("Convert2MethodRef")
public class UsePerson {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Paul", "George", "Ringo");
Expand Down Expand Up @@ -49,7 +47,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 All @@ -66,5 +64,13 @@ public static void main(String[] args) {
.map(Person::new)
.collect(Collectors.toCollection(LinkedList::new));
System.out.println(linkedPersons);

// Collectors.toMap(keyMapper, valueMapper)
Map<String, String> map = fullNames.stream()
.map(name -> name.split(" "))
.collect(Collectors.toMap(
name -> name[0], // keyMapper
name -> name[1]));// valueMapper
map.forEach((key, value) -> System.out.println(key + " -> " + value));
}
}
3 changes: 0 additions & 3 deletions src/main/java/lazy/LazyStreams.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package lazy;

import java.util.logging.Logger;
import java.util.stream.IntStream;

public class LazyStreams {
private static final Logger logger = Logger.getLogger(LazyStreams.class.getName());

public static int multByTwo(int n) {
System.out.printf("Inside multByTwo with arg %d on thread %s%n",
n, Thread.currentThread().getName());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/streams/FlatMapDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public static void main(String[] args) {
// stream() on an empty collection already returns an empty stream
customers.stream()
.flatMap(customer ->
customer.getOrders().size() == 0 ? Stream.empty() :
customer.getOrders().isEmpty() ? Stream.empty() :
customer.getOrders().stream())
.forEach(System.out::println);

}
}
6 changes: 4 additions & 2 deletions src/test/java/interfaces/CompanyEmployeeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

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

// Create a class called CompanyEmployee that implements both
// the Company and Employee interfaces
// Implement the necessary methods
Expand All @@ -11,7 +13,7 @@ public class CompanyEmployeeTest {

@Test
public void getName() {
// CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons");
// assertEquals("Peter Gibbons works for Initech", emp.getName());
CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons");
assertEquals("Peter Gibbons works for Initech", emp.getName());
}
}

0 comments on commit 03b8554

Please sign in to comment.