Guava: Set + Function = Map?

Creating a Map from a Set and a Function Here are two classes that should each do the job. The first just shows a map view of the set, while the second can write values back to the set through a special interface. Call Syntax: Map<K,V> immutable = new SetBackedMap<K,V>(Set<K> keys, Function<K,V> func); Map<K,V> mutable … Read more

How to convert Map to Map ? (option : using guava)

@Vivin’s answer is correct, but I think it’s useful to explain why Guava doesn’t have any method to allow you to transform the keys of a Map (or to transform a Set at all). All of Guava’s methods for transforming and filtering produce lazy results… the function/predicate is only applied when needed as the object … Read more

ListenableFuture, FutureCallback and timeouts

Update: This has been added to Guava as Futures.withTimeout(). Internally, we have a makeTimeoutFuture method that takes a Future as input and returns a new Future that will have the same result unless the original hasn’t completed by a given deadline. If the deadline expires, the output Future has its result set to a TimeoutException. … Read more

Most efficient way to find the collection of all ids in a collection of entities

Assuming you have class Entity { final long id; final String data; public long getId() { return id; } public String getData() { return data; } Entity(long id, String data) { this.id = id; this.data = data; } } In Java 8 you can write Collection<Entity> entities = Arrays.asList(new Entity(1, “one”), new Entity(11, “eleven”), new … Read more

@Nullable input in Google Guava Function interface triggers FindBugs warning

Your implementation is wrong 😉 Basically docs says (I’ll paraphrase and emphasise): @throws NullPointerException if input is null and the concrete function implementation does not accept null arguments By implementing your function you must decide if it accepts nulls or not. In first case: private static final class Example implements Function<MyBean, String> { @Override @Nullable … Read more

Get minvalue of a Map(Key,Double)

You can use the standard Collections#min() for this. Map<String, Double> map = new HashMap<String, Double>(); map.put(“1.1”, 1.1); map.put(“0.1”, 0.1); map.put(“2.1”, 2.1); Double min = Collections.min(map.values()); System.out.println(min); // 0.1 Update: since you need the key as well, well, I don’t see ways in Collections or Google Collections2 API since a Map is not a Collection. The … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)