Guava

Google Guava: How to use ImmutableSortedMap.naturalOrder?

拥有回忆 提交于 2019-12-23 21:19:40
问题 I'm using Google Guava r08 and JDK 1.6.0_23. I want to create an ImmutableSortedMap using a builder. I know I can create the builder like this: ImmutableSortedMap.Builder<Integer, String> b1 = new ImmutableSortedMap.Builder<Integer, String>(Ordering.natural()); and then use that to build maps, for example: ImmutableSortedMap<Integer, String> map = b1.put(1, "one").put(2, "two").put(3, "three").build(); I noticed that class ImmutableSortedMap has a method naturalOrder() that returns a Builder

Google Collections Distinct Predicate

寵の児 提交于 2019-12-23 19:28:07
问题 How would one implement a distinct predicate for use with the Google Collections Collections2.filter method? 回答1: If I understand you correctly, I'm not sure that Predicate is the right solution here: Creating a predicate like that would require maintaining some sort of state (ie: maintaining a Set of things it has seen already). This is explicitly advised against in the javadoc. The usual way to get the distinct items in a collection would be to just add them all to a set. ie: Set<T>

Guava CacheBuilder: imply additional conditions to entity removal

ぃ、小莉子 提交于 2019-12-23 17:06:58
问题 I want: Remove entity when both conditions are true timeout expired some external conditions are true Question: How should I imply additional removal conditions besides timeout? Or how can I restore entity from removal listener (see code below)? My code (which already remove based on timeout only): LoadingCache<String, Integer> ints = CacheBuilder.newBuilder() .maximumSize(10000) .expireAfterAccess(ACCESS_TIMEOUT, TimeUnit.MILLISECONDS) .removalListener( new RemovalListener() { public void

Incompatible library version selenium / guava

╄→尐↘猪︶ㄣ 提交于 2019-12-23 14:38:14
问题 My app has a hard dependency on com.google.guava:guava:23.3 or superior +--- com.github.ben-manes.caffeine:guava:2.6.0 | +--- com.github.ben-manes.caffeine:caffeine:2.6.0 | \--- com.google.guava:guava:23.3-jre (*) But I am also using org.seleniumhq.selenium:selenium-java:3.0.1 which is incompatible with guava versions > 22.0 as discussed here: https://github.com/SeleniumHQ/selenium/issues/4381 I am pretty new to Java, what is the best course of action here ? I have looked into class loaders

Elegant solution for filtering a list to be unique for given object attribute

馋奶兔 提交于 2019-12-23 14:20:36
问题 I have a flattened multi-map (a list) of objects which I would like to convert to a list where a 'key' attribute / field (e.g. name below) is unique amongst all entries. In the case where multiple entries have the same key ( name ) the entry with the largest creationDate field should be picked. Example: List<Object> myList = [ {name="abc", age=23, creationDate = 1234L}, {name="abc", age=12, creationDate = 2345L}, {name="ddd", age=99, creationDate = 9999L} ] Should be converted to: List<Object

Maven 3.0.4 NoSuchMethod: … java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(..)

女生的网名这么多〃 提交于 2019-12-23 12:15:23
问题 I've installed Maven 3.0.4 with Homebrew and whenever I run the mvn command I get the following: Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableSet; From Googling the error it seems to be related some sort of Google guava tool/library -- which I know nothing about. I attempted to replaced some Jars in the /bin directory of maven but it had no affect Any help? I don't know where

How should I implement Guava cache when I plan to cache multiple values efficiently?

冷暖自知 提交于 2019-12-23 12:11:55
问题 I have a Java class that has a Guava LoadingCache<String, Integer> and in that cache, I'm planning to store two things: the average time active employees have worked for the day and their efficiency. I am caching these values because it would be expensive to compute every time a request comes in. Also, the contents of the cache will be refreshed ( refreshAfterWrite ) every minute. I was thinking of using a CacheLoader for this situation, however, its load method only loads one value per key.

com.google.common.collect.Sets.SetView bug or feature?

馋奶兔 提交于 2019-12-23 10:39:50
问题 Hello I've this piece of code: public static void main(String[] args) { Set<Integer> set1 = new HashSet<Integer>(); Set<Integer> set2 = new HashSet<Integer>(); set1.add(1); set1.add(2); set1.add(3); set1.add(4); set1.add(5); set2.add(4); set2.add(5); set2.add(6); set2.add(7); set2.add(8); SetView<Integer> x = Sets.intersection(set1, set2); set1.removeAll(x); set2.removeAll(x); } and it throws Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap

Why do I need adding artifact JSR305 to use Guava 14+?

≯℡__Kan透↙ 提交于 2019-12-23 10:08:14
问题 While looking for informations on stackoverflow, I have seen a question similar to mine, but with no real answer here. I need migrating my maven project from guava 11.0.2 to guava 14 or higher (I need RangeSet). I updated my maven pom with the dependency: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>14.0</version> </dependency> I then run the maven build, and got this error: [ERROR] xxx.java: cannot find symbol [ERROR] symbol : class Nonnull [ERROR]

Guava EventBus unit tests

≡放荡痞女 提交于 2019-12-23 09:58:42
问题 I have a simple Guava EventBus with one simple event and one simple listener. My question is what is the test pattern to check if the listener method is invoked once the event is posted. 回答1: I would suggest that testing that the EventBus works properly is not a UNIT test you should be writing. One of the advantages of using a library (at least of using one you trust) is that the unit tests have been written by the library provider. So please don't waste your time verifying that the Google