Guava

Sorting Guava tables in descending order based on values

纵然是瞬间 提交于 2019-12-24 08:18:27
问题 I am planning to use guava tables for storing my values in the table format. I would like to know some function which performs the descending order sorting based on the value in the table ... Could you people throw some views about this. Thank you. 回答1: Just like with maps, you should copy the cellSet() , sort the cells by the values, and then put that into an order-preserving ImmutableTable . Ordering<Table.Cell<String, String, Integer>> comparator = new Ordering<Table.Cell<String, String,

How to make concurrent hash map thread safe with get and put as an atomic operation?

ⅰ亾dé卋堺 提交于 2019-12-24 07:56:50
问题 Is my below method thread safe? This method is in Singleton class. private static final Map<String, PreparedStatement> holder = new ConcurrentHashMap<>(); public BoundStatement getStatement(String cql) { Session session = TestUtils.getInstance().getSession(); PreparedStatement ps = holder.get(cql); if(ps == null) { // If "ps" is already present in cache, then we don't have to synchronize and make threads wait. synchronized { ps = holder.get(cql); if (ps == null) { ps = session.prepare(cql);

Guava: construct a Multimap by inverting a Map

会有一股神秘感。 提交于 2019-12-24 06:28:29
问题 why does Guava doesn't have the following factory call to create a MultiMap from a normal Map? public static <K,V> MultiMap<K,V> invertMap(Map<V,K> map); I have program-names mapped to an integer of how often they were called. I'd like to invert this, so that i can ultimately construct a TreeMap, sorted by call-count, which then are the keys leading to one or multiple program-names. 回答1: How about: public static <K,V> Multimap<K,V> invertMap(Map<V,K> map) { return Multimaps.invertFrom

Guava: construct a Multimap by inverting a Map

╄→尐↘猪︶ㄣ 提交于 2019-12-24 06:27:59
问题 why does Guava doesn't have the following factory call to create a MultiMap from a normal Map? public static <K,V> MultiMap<K,V> invertMap(Map<V,K> map); I have program-names mapped to an integer of how often they were called. I'd like to invert this, so that i can ultimately construct a TreeMap, sorted by call-count, which then are the keys leading to one or multiple program-names. 回答1: How about: public static <K,V> Multimap<K,V> invertMap(Map<V,K> map) { return Multimaps.invertFrom

Guava - how can I generate a random number using Range?

余生长醉 提交于 2019-12-24 05:30:36
问题 Google's Guava library provides a great class called Range, it has many useful methods like greaterThan(x) , open(x,y) , etc. I am wondering if there is any way of applying such method to generate a random number within a Range ? 回答1: I would not suggest using a range for this basic application. The easiest method to use is the already implemented Random class. Here is how to use the class: For getting a random integer of any value: Random r = new Random(); r.nextInt(); For getting a random

Can a ListenableFuture chain handle inner ExecutionException?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 03:08:15
问题 I am provided with an api ( fnGrpc ) that performs a gRPC call and returns a ListenableFuture that resolves to some value v (the implementation of which is fixed and unmodifiable). I want to provide a helper function ( fnHelper ) that: does some transformational processing on the gRPC result and itself returns a ListenableFuture that resolves to the transformed value t1 . handles failure of the gRPC call, and returns some other value t2 instead of having fnHelper 's caller see an

Can a ListenableFuture chain handle inner ExecutionException?

穿精又带淫゛_ 提交于 2019-12-24 03:08:13
问题 I am provided with an api ( fnGrpc ) that performs a gRPC call and returns a ListenableFuture that resolves to some value v (the implementation of which is fixed and unmodifiable). I want to provide a helper function ( fnHelper ) that: does some transformational processing on the gRPC result and itself returns a ListenableFuture that resolves to the transformed value t1 . handles failure of the gRPC call, and returns some other value t2 instead of having fnHelper 's caller see an

Initialize an ImmutableMap with explicit types?

﹥>﹥吖頭↗ 提交于 2019-12-24 01:32:31
问题 I need to initialize an ImmutableMap (guava 21.0) and I need it to resolve to a Map> for this example I will just use String. So I have: import com.google.common.collect.ImmutableMap; public class MyClass { private Map<String,String> testMap = ImmutableMap<String,String>.builder().put("a","b").build(); and on that last line I get a huge amount of compiler errors. The use of ImmutableMap<String,String>.of() gets the same result. If I remove I just get one error, a type mismatch. How can I use

Initialize an ImmutableMap with explicit types?

為{幸葍}努か 提交于 2019-12-24 01:31:51
问题 I need to initialize an ImmutableMap (guava 21.0) and I need it to resolve to a Map> for this example I will just use String. So I have: import com.google.common.collect.ImmutableMap; public class MyClass { private Map<String,String> testMap = ImmutableMap<String,String>.builder().put("a","b").build(); and on that last line I get a huge amount of compiler errors. The use of ImmutableMap<String,String>.of() gets the same result. If I remove I just get one error, a type mismatch. How can I use

java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.ascii()

自作多情 提交于 2019-12-23 23:13:49
问题 I got this error on glassfish server java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.ascii()Lcom/google/common/base/CharMatcher; at com.google.common.io.BaseEncoding$Alphabet.<init>(BaseEncoding.java:453) at com.google.common.io.BaseEncoding$Base64Encoding.<init>(BaseEncoding.java:892) at com.google.common.io.BaseEncoding.<clinit>(BaseEncoding.java:317) at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:133) at utils.GoogleCouldBucket_Utils.initGoogleBucket