Skip to content

Instantly share code, notes, and snippets.

View liviutudor's full-sized avatar

Liviu Tudor liviutudor

View GitHub Profile
@liviutudor
liviutudor / Sample.java
Created June 28, 2017 07:05
Sample on how to process a http request sequentially in Java
@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);
}
@liviutudor
liviutudor / FindExtractMaxStreams.java
Last active April 1, 2017 01:06
Find elements, map and extract in java using streams
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");
@liviutudor
liviutudor / FindExtractMaxNoStreams.java
Created April 1, 2017 00:56
Find by name, extract salary and compute max in java, no streams
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;
}
@liviutudor
liviutudor / MapStream.java
Created April 1, 2017 00:50
`Map` on java streams
public Collection<Double> extract(Collection<Employee> collection) {
return collection.stream().map(c -> c.salary).collect(Collectors.toList());
}
@liviutudor
liviutudor / NoStreamsMap.java
Created April 1, 2017 00:49
Implement a `map`-like without streams
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;
}
@liviutudor
liviutudor / FindBobWithStreams.java
Created April 1, 2017 00:37
Find element in a collection in java using streams
// 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());
}
@liviutudor
liviutudor / FindBobNoStreams.java
Created April 1, 2017 00:32
Find in a collection, java without streams
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
@liviutudor
liviutudor / MaxWithStreams.java
Created April 1, 2017 00:24
Find max in a list of java classes using streams
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));
}
@liviutudor
liviutudor / MaxNoStreams.java
Created April 1, 2017 00:20
Find max from a list of structures in Java without using streams
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();
@liviutudor
liviutudor / MaxStreams.java
Created April 1, 2017 00:11
Finding the max in a java collection using streams
public Optional<Integer> max(Collection<Integer> collection) {
return collection.stream().max(Integer::max);
}
// usage
List<Integer> list = ...;
int m = max(list).orElse(0);