This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Collection<Double> extract(Collection<Employee> collection) { | |
return collection.stream().map(c -> c.salary).collect(Collectors.toList()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Optional<Double> max(Collection<Double> collection) { | |
if (collection == null || collection.isEmpty()) return Optional.empty(); | |
Iterator<Double> iter = collection.iterator(); | |
Double max = iter.next(); | |
while (iter.hasNext()) { | |
Double n = iter.next(); | |
if (n > max) max = n; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Optional<Double> findExtractAndMax(Collection<Employee> collection, String name) { | |
return collection.stream().filter(e -> e.firstName.equals(name)).map(e -> e.salary).max((a, b) -> (int) Math.signum(a - b)); | |
} | |
//usage | |
double sal = findExtractAndMax(collection, "Bob"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GET | |
public Response processRequest( @QueryParam("id")String id, @QueryParam("userId")String userId ) { | |
Record retrieve = database.retrieveRecord(id); | |
logger.writeEntryFor( id, userId ); | |
UserProfile profile = userStore.retrieve(userId); | |
generateAdResponse(retrieve, profile); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GET | |
public Observable<Response> processRequest( @QueryParam("id")String id, @QueryParam("userId")String userId ) { | |
return Observable.zip( | |
Observable.fromCallable(() -> database.retrieveRecord(id)), | |
Observable.fromCallable(() -> userStore.retrieve(userId)), | |
Observable.fromCallable(() -> logger.writeEntryFor(id, userId)), | |
this::generateAdResponse | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
public class JacksonBean { | |
private final int value; | |
private final String another; | |
@JsonCreator | |
public JacksonBean(@JsonProperty("value") int value, @JsonProperty("another") String another) { | |
this.value = value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.beans.ConstructorProperties; | |
public class JacksonBean { | |
private final int value; | |
private final String another; | |
@ConstructorProperties({"value", "another"}) | |
public JacksonBean(int value, String another) { | |
this.value = value; | |
this.another = another; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Sortable | |
import groovy.transform.TupleConstructor | |
@Sortable | |
@TupleConstructor | |
class BeanOne { | |
String firstName | |
String familyName | |
public String getFullName() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.Sortable | |
import groovy.transform.TupleConstructor | |
@Sortable | |
@TupleConstructor | |
class BeanTwo { | |
int yob | |
public int getAge() { | |
new Date().year - yob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DelegationBean { | |
@Delegate | |
BeanOne person | |
@Delegate | |
BeanTwo dateOfBirth | |
static void main(String... args) { | |
DelegationBean one = new DelegationBean(person: new BeanOne("Liviu", "Tudor"), dateOfBirth: new BeanTwo(yob: 1975)) | |
DelegationBean two = new DelegationBean(person: new BeanOne("William", "Shakespeare"), dateOfBirth: new BeanTwo(yob: 1564)) |