Skip to content

Instantly share code, notes, and snippets.

Avatar

Liviu Tudor liviutudor

View GitHub Profile
@liviutudor
liviutudor / Sample.java
Created Jun 28, 2017
Sample on how to process a http request sequentially in Java
View Sample.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 Apr 1, 2017
Find elements, map and extract in java using streams
View FindExtractMaxStreams.java
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 Apr 1, 2017
Find by name, extract salary and compute max in java, no streams
View FindExtractMaxNoStreams.java
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 Apr 1, 2017
`Map` on java streams
View MapStream.java
public Collection<Double> extract(Collection<Employee> collection) {
return collection.stream().map(c -> c.salary).collect(Collectors.toList());
}
@liviutudor
liviutudor / NoStreamsMap.java
Created Apr 1, 2017
Implement a `map`-like without streams
View NoStreamsMap.java
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 Apr 1, 2017
Find element in a collection in java using streams
View FindBobWithStreams.java
// 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 Apr 1, 2017
Find in a collection, java without streams
View FindBobNoStreams.java
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 Apr 1, 2017
Find max in a list of java classes using streams
View MaxWithStreams.java
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 Apr 1, 2017
Find max from a list of structures in Java without using streams
View MaxNoStreams.java
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 Apr 1, 2017
Finding the max in a java collection using streams
View MaxStreams.java
public Optional<Integer> max(Collection<Integer> collection) {
return collection.stream().max(Integer::max);
}
// usage
List<Integer> list = ...;
int m = max(list).orElse(0);
You can’t perform that action at this time.