Guava

How to get all values from guava LoadingCache without passing any keys

大城市里の小女人 提交于 2019-12-23 08:03:31
问题 I am using Guava LoadingCache to cache some of the results. Using load method I fetch results from other source and put into cache using ' put(key,value) '. Now the problem I am trying to solve is: I want to get all the available results in that cache with out passing any keys. Because I am interested in taking all the values presented in the cache at that time regardless of any specific keys. getall(Iterable<?> keys) or getAllPresent(Iterable<?> keys) methods are there but those are

Create Weak Multimap with Google Collections

那年仲夏 提交于 2019-12-23 07:28:32
问题 Is there an equivalent to the nice MapMaker for MultiMaps? currently i create the cache like this: public static Map<Session,List<Person>> personCache = new MapMaker().weakKeys().makeMap(); the whole point of MultiMap is to avoid the nested List Values. is there any way to construct the multimap with weak keys? 回答1: Unfortunately not. Yet. Could you file a MultimapMaker feature request in our issues db? http://google-collections.googlecode.com 来源: https://stackoverflow.com/questions/737060

Defects of Immutable collections of Guava?

这一生的挚爱 提交于 2019-12-23 05:26:35
问题 I am not sure the defects of Immutable collections I understand is correct, so I list them in this answer. Hope someone corrects me here. a): Comparing to Collections.unmodifiableXXX(), ImmutableXXX.copyOf() loses the source collection feature . For example, when a linkedList is put into ImmutableList.copyOf(), the ImmutableList is not linked anymore. Same as Tree based collection. b): People think Collections.unmodifiableXXX just uses same reference of source collection, so once the source

Can you use a functor/functional programming to group a list in Java 7 (and count its elements per group)?

不打扰是莪最后的温柔 提交于 2019-12-23 04:24:38
问题 Can you group List<TypeEnum> types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A)); into a Map<TypeEnum, Integer> countPerType; , using functors (e.g. Google's Guava, Apache's Commons Functor) pre Java 8? I'm trying to get my head around functional programming, but am unsure if that sort of thing would actually be possible (as I'm not just mapping a collections value, but trying to aggregate)? In imperative style, I would have done something like this: public Map<TypeEnum,

Define guava HashBasedTable/Table in spring xml config

霸气de小男生 提交于 2019-12-23 02:31:39
问题 I'm trying to create and populate a guava HashBasedTable in spring xml config file but I haven't been able to. My table looks like this: Table<String, Foo, Bar> myTable; And I've tried this in my xml but don't know how put new value into the table: <property name="myTable"> <bean class="com.google.common.collect.HashBasedTable" factory-method="create"> <!--- how do I insert value in here??? --> </bean> </property> 回答1: If you want to do this exclusively in xml, it's a bit tricky: I see guava

What's the best way to sum two Map<String,String>?

只谈情不闲聊 提交于 2019-12-22 19:20:14
问题 I have the following maps. Map<String,String> map1= new HashMap<String, String>(){{ put("no1","123"); put("no2","5434"); put("no5","234");}}; Map<String,String> map1 = new HashMap<String, String>(){{ put("no1","523"); put("no2","234"); put("no3","234");}}; sum(map1, map2); I want to join them to one, summing up similar keyed values together. What;s the best way I could do it using java 7 or guava libraries ? expected output Map<String, String> output = { { "no1" ,"646"}, { "no2", "5668"}, {

重试利器Guava Retrying

£可爱£侵袭症+ 提交于 2019-12-22 17:48:49
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 重试的使用场景 在很多业务场景中,为了排除系统中的各种不稳定因素,以及逻辑上的错误,并最大概率保证获得预期的结果,重试机制都是必不可少的。 尤其是调用远程服务,在高并发场景下,很可能因为服务器响应延迟或者网络原因,造成我们得不到想要的结果,或者根本得不到响应。这个时候,一个优雅的重试调用机制,可以让我们更大概率保证得到预期的响应。 sequenceDiagram Client->>Server:{"msg":"hello,server"} Note right of Server: busying ...... Client->>Server:{"msg":"hello,server"} Server-->>Client:{"Exception":"500"} Note right of Server: busying ...... loop ServerBlock Server -->> Server: Too busy to deal with so many requests... end Client->>Server:{"msg":"hello,server"} activate Server Server-->>Client:{"msg":"hello,client"} deactivate

Guava Retrying解析

守給你的承諾、 提交于 2019-12-22 17:38:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、重试场景 远程调用遇到限流或者超时,但是在并发的情况下需要考虑请求的幂等,不是这里要讨论的问题 1. 什么情况下重试 -- 有异常或者返回Success是false时 重试策略:异常校验、结果校验 2. 什么情况下结束 -- 重试3次 终止策略 3. 等待多久重试 -- 200ms 等待策略 4. 监听重试过程 二、 基本用法 Retryer<Boolean> retryer = RetryerBuilder .<Boolean>newBuilder() // 抛出runtime异常、checked异常时都会重试,但是抛出error不会重试。 .retryIfException() // 自定义 指定返回值 也需要重试:返回false也需要重试 .retryIfResult(Predicates.equalTo(false)) // 重试时间间隔 .withWaitStrategy(WaitStrategies.fixedWait(200, TimeUnit.MILLISECONDS)) // 尝试次数 .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); retryer.call(() -> {}); 1.

Google Guava Cache - Change the eviction timeout values during run time

浪尽此生 提交于 2019-12-22 13:56:10
问题 I am using the following: LoadingCache<String, Long> inQueueLoadingCache = CacheBuilder.newBuilder() .expireAfterWrite(120, TimeUnit.SECONDS) .removalListener(inQueueRemovalListener) .build(inQueueCacheLoader); After every 120 seconds, the cache entries are evicted and it works as expected. My question is: How do I change the timeout value, say from 120 to 60 seconds, for the current cache? What will happen to the cache entries during this change? 回答1: Short answer: you can't change eviction

Why no SortedMultiset in Google Collections?

旧时模样 提交于 2019-12-22 08:10:52
问题 Google Collections contains the Multiset interface and the TreeMultiset class, but I was surprised to find that there is no corresponding SortedMultiset interface. Something like that would be very useful for modelling discrete probability distributions. Before I attempt to implement it myself, I would like to know if there is a specific reason for leaving it out, e.g. likely violation of Multiset or Collection invariants, or inherent performance problems etc. Edit : I didn't realise it