How to get max() element from List in Guava

Ordering<Item> o = new Ordering<Item>() { @Override public int compare(Item left, Item right) { return Ints.compare(left.price, right.price); } }; return o.max(list); It’s as efficient as it can be: it iterates through the items of the list, and returns the first of the Items having the maximum price: O(n).

java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

The only plausible explanation to this problem is there is an older version of HttpCore on the classpath (unless you also want to consider a possibility of green men from Mars messing with your computer remotely from a flying saucer). You can add this snippet to your code to find out what jar the class …

Read more

Pulling values from a Java Properties file in order?

Extend java.util.Properties, override both put() and keys(): import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Properties; import java.util.HashMap; public class LinkedProperties extends Properties { private final HashSet<Object> keys = new LinkedHashSet<Object>(); public LinkedProperties() { } public Iterable<Object> orderedKeys() { return Collections.list(keys()); } public Enumeration<Object> keys() { return Collections.<Object>enumeration(keys); } public Object put(Object key, Object …

Read more

Java enum elements with spaces?

You can’t put a space in the middle of an identifier. Doing so ends that identifier and the parser assumes whatever comes next is a valid token in that statement’s context. There are few (if any) places that would be legal. Conventional Java value names would be: INDIA, // Or India, RUSSIA, // Russia, NORTH_AMERICA; …

Read more