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
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
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 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 Collection<Double> extract(Collection<Employee> collection) { | |
Collection<Double> list = new ArrayList<>(); | |
if( collection == null || collection.isEmpty() ) return list; | |
for( Employee e : collection ) { | |
list.add(e.salary); | |
} | |
return list; | |
} |
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
// this returns a list | |
public Collection<Employee> findName(Collection<Employee> collection, String name) { | |
return collection.stream().filter(e -> e.firstName.equals(name)).collect(Collectors.toList()); | |
} | |
// if we want to return a set (and therefore guarantee uniqueness) | |
public Collection<Employee> findName(Collection<Employee> collection, String name) { | |
return collection.stream().filter(e -> e.firstName.equals(name)).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 Collection<Employee> findName(Collection<Employee> collection, String name) { | |
List<Employee> list = new ArrayList<>(); | |
if (collection == null || collection.isEmpty()) return list; | |
for (Employee e : collection) { | |
if (e.firstName.equals(name)) list.add(e); | |
} | |
return list; | |
} | |
// usage |
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
static class Employee { | |
String firstName; | |
String surname; | |
double salary; //yeah, right! | |
} | |
public Optional<Employee> max(Collection<Employee> collection) { | |
return collection.stream().max((e1, e2) -> (int) Math.signum(e1.salary - e2.salary)); | |
} |
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
static class Employee { | |
String firstName; | |
String surname; | |
double salary; //yeah, right! | |
} | |
public Optional<Employee> max(Collection<Employee> collection) { | |
if (collection == null || collection.isEmpty()) return Optional.empty(); | |
Iterator<Employee> employeeIterator = collection.iterator(); |
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<Integer> max(Collection<Integer> collection) { | |
return collection.stream().max(Integer::max); | |
} | |
// usage | |
List<Integer> list = ...; | |
int m = max(list).orElse(0); |