Guava

Guava SetMultimap not serializable (due to not serializable WrappedSet)

大城市里の小女人 提交于 2020-01-02 09:57:30
问题 I'm often using java serialization, which is very usefull to store a complete object hierarchy. When trying to serialize a SetMultimap, I got an exception saying that that AbstractMultimap.WrappedSet is not serializable. How do guava users workaround with this problem? Thanks in advance, 回答1: The views of elements of a multimap (such as the collections returned from get methods, the asMap view, etc.) are intentionally not serializable. However, it isn't true that a SetMultimap implementation

google common cache - default value of maximumSize (and other “optional” settings) - want a cache that uses all “available” memory

为君一笑 提交于 2020-01-02 08:35:48
问题 I just found Guava by searching for a cache API (it fits perfectly for my needs). But one question arose on reading the wiki and Javadoc - what are the default values of settings the CacheBuilder can take? The Javadoc states "These features are all optional" and "Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind." In my opinion, a good default for maximumSize would be relative to Runtime.getRuntime()

google common cache - default value of maximumSize (and other “optional” settings) - want a cache that uses all “available” memory

╄→гoц情女王★ 提交于 2020-01-02 08:35:38
问题 I just found Guava by searching for a cache API (it fits perfectly for my needs). But one question arose on reading the wiki and Javadoc - what are the default values of settings the CacheBuilder can take? The Javadoc states "These features are all optional" and "Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind." In my opinion, a good default for maximumSize would be relative to Runtime.getRuntime()

Is it safe to reinsert the entry from Guava RemovalListener?

南笙酒味 提交于 2020-01-02 01:08:54
问题 I've got a Guava Cache (or rather, I am migrating from MapMaker to Cache ) and the values represent long-running jobs. I'd like to add expireAfterAccess behavior to the cache, as it's the best way to clean it up; however, the job may still be running even though it hasn't been accessed via the cache in some time, and in that case I need to prevent it from being removed from the cache. I have three questions: Is it safe to reinsert the cache entry that's being removed during the

HashMap returned by Maps.newHashMap vs new HashMap

霸气de小男生 提交于 2020-01-02 00:43:08
问题 I am trying Guava for the first time and I find it really awesome. I am executing few parameterized retrieve queries on a Spring jdbc template. The method in the DAO ( AbstractDataAccessObject ) goes like this. No problem here. public Map<String,Object> getResultAsMap(String sql, Map<String,Object> parameters) { try { return jdbcTemplate.queryForMap(sql, parameters); } catch (EmptyResultDataAccessException e) { //Ignore if no data found for this query logger.error(e.getMessage(), e); } return

Java Hash Multi Map (key with multiple values) Implementation

前提是你 提交于 2020-01-01 16:56:52
问题 From here, I found that Colt's OpenIntIntHashMap and Trove's TIntIntHashMap give better performance and memory uses than Java's built in HashMap or Guava's HashMultimap . Do Colt's OpenIntIntHashMap or Trove's TIntIntHashMap allow keys with multiple values, as with HashMultimap ? If not what is a nice way to implement a HashMultimap that can achieve Colt's or Trove's performance and memory efficiency? Note: I have tested Guava's HashMultimap , but its performance and memory efficiency seems

JSON: Serialize Guava Optional

ぃ、小莉子 提交于 2020-01-01 12:31:09
问题 Is there a Json Serializer/Deserializer for com.google.common.base.Optional? Out of the box this doesn't seem to work with Jackson, see below: package com.example; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import com.google.common.base.Optional; public class TestClass { public Optional<String> myString; public TestClass() { myString = Optional.of(

Guava’s RateLimiter per minutes instead of seconds?

こ雲淡風輕ζ 提交于 2020-01-01 08:27:18
问题 I'm trying to rate-limit the the number of accounts a user can create with my REST API. I would have liked to use Guava's RateLimiter to only allow an IP to create, let's say, 5 accounts within 10 minutes, but the RateLimiter.create method only takes a double specifying the number of permits "per second". Is there a way to configure RateLimiter to release permits at a granularity greater than one second? 回答1: From the RateLimiter.create javadoc: When the incoming request rate exceeds

Java 8 collector for Guava Immutable Table

 ̄綄美尐妖づ 提交于 2020-01-01 07:04:15
问题 Use case: Process list of string via method which returns ImmutableTable of type {R,C,V} . For instance ImmutableTable of {Integer,String,Boolean} process(String item){...} Collect the result i.e, merge all results and return ImmutableTable . Is there a way to achieve it? Current implementation (as suggested by Bohemian): How about using parallel stream ? Is there any concurrency issues in the below code? With Parallel stream i an getting "NullPointerException at index 1800" on tableBuilder

Java Generics Wildcards Question

情到浓时终转凉″ 提交于 2020-01-01 04:15:07
问题 I'm facing some problems with Generics when using Google Guava's excellent Multimap. I have a type Handler defined as such public interface Handler<T extends Serializable> { void handle(T t); } In another class I've defined a multimap that maps a String to a collection of Handlers. private Multimap<String, Handler<? extends Serializable>> multimap = ArrayListMultimap.create(); Now when I try to do stuff with the multimap, I'm getting compiler errors. My first attempt looked like this: public