Skip to content

Instantly share code, notes, and snippets.

Avatar

Liviu Tudor liviutudor

View GitHub Profile
@liviutudor
liviutudor / gist:5962008
Created Jul 9, 2013
Failed to install `rpm` on MacOS X 10.8.4
View gist:5962008
Output of `brew install`:
~ $ HOMEBREW_MAKE_JOBS=1 VERBOSE=1 brew install rpm 2>&1
==> Installing rpm dependency: rpm2cpio
==> Downloading http://svnweb.freebsd.org/ports/head/archivers/rpm2cpio/files/rpm2cpio?revision=259745&view=co
Already downloaded: /Library/Caches/Homebrew/rpm2cpio-1.3
Error: rpm2cpio?revision=259745 does not exist
~ $
~ $
~ $
@liviutudor
liviutudor / WrongWayToUseConfiguration.java
Last active Mar 29, 2017
Wrong way to use Governator's @configuration annotation
View WrongWayToUseConfiguration.java
public class MyClass {
@Configuration("path.prefix") // this is wrong!
private static final String PATH_PREFIX = null;
private String fileName;
public MyClass(String fileName) {
this.fileName = PATH_PREFIX + fileName;
}
}
@liviutudor
liviutudor / RightWayToUseConfiguration.java
Created Mar 29, 2017
Right way of using Governator's @configuration annotation
View RightWayToUseConfiguration.java
public class MyClass {
@Configuration("path.prefix")
private String pathPrefix;
private String fileName;
public MyClass(String fileName) {
this.fileName = fileName; //here we simply store it
}
@liviutudor
liviutudor / NoStreams.java
Last active Apr 1, 2017
Find max int in a collection in java without using streams
View NoStreams.java
public Integer max(Collection<Integer> collection) {
if( collection == null || collection.isEmpty() ) return null;
Iterator<Integer> i = collection.iterator();
int max = i.next();
while (i.hasNext()) {
int n = i.next();
if (max < n) max = n;
}
return max;
}
@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);
@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 / 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 / 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 / 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 / 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;
}
You can’t perform that action at this time.