Guava

Is it possible to Iterate over a guava Cache in order of insertion/access?

谁说胖子不能爱 提交于 2019-12-22 08:06:32
问题 I'm trying to use a Guava Cache as a replacement for the ConcurrentLinkedHashMap. However I found that while the ConcurrentLinkedHashMap allowed me to iterate over the map in order of Insertion, Guava's asMap() method doesn't return elements in any particular order. Am I missing something, or is this functionality simply not available? Example (trying to print the keys, the values, and the entries): Cache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(10).initialCapacity(10)

Java Generics type conversion puzzle

只谈情不闲聊 提交于 2019-12-22 05:42:14
问题 I'm attempting to use Google's Guava ImmutableSet class to create a set of immutable classes with timelike properties (java.util.Date, and org.joda.time.DateTime). private static final ImmutableSet<Class<?>> timeLikeObjects = ImmutableSet.of(Date.class, DateTime.class); I'm completely stumped as to why I'm getting this compiler error (Java 1.6 in eclipse). Type mismatch: cannot convert from ImmutableSet<Class<? extends Object&Serializable&Comparable<? extends Comparable<?>>>> to ImmutableSet

Is it possible to apply a function to a collection using Java Guava?

非 Y 不嫁゛ 提交于 2019-12-22 04:55:07
问题 I want to apply a function to a collection, map, etc, using Guava. Basically, I need to resize the rows and columns of a Table separately so all rows and columns are of equal size, doing something like this: Table<Integer, Integer, Cell> table = HashBasedTable.create(); Maps.transformValues(table.columnMap(), new ResizeFunction(BlockDimension.WIDTH)); Maps.transformValues(table.rowMap(), new ResizeFunction(BlockDimension.HEIGHT)); public interface Cell { int getSize(BlockDimension dimension);

Case Insensitive sorting using Google Guava

佐手、 提交于 2019-12-22 04:43:11
问题 Current I am using the following 2 pieces of code in 2 different places to create a sorted, immutable list. return Ordering.natural().immutableSortedCopy(iterable); and return Ordering.usingToString().immutableSortedCopy(machines); However, this makes the 'ordering' case sensitive . How do I use the guava apis to make a case- insensitive sorted immutable list? 回答1: I believe you will need to use the from method with the String.CASE_INSENSITIVE_ORDER comparator, like this. return Ordering.from

Case Insensitive sorting using Google Guava

牧云@^-^@ 提交于 2019-12-22 04:43:01
问题 Current I am using the following 2 pieces of code in 2 different places to create a sorted, immutable list. return Ordering.natural().immutableSortedCopy(iterable); and return Ordering.usingToString().immutableSortedCopy(machines); However, this makes the 'ordering' case sensitive . How do I use the guava apis to make a case- insensitive sorted immutable list? 回答1: I believe you will need to use the from method with the String.CASE_INSENSITIVE_ORDER comparator, like this. return Ordering.from

Java: external class for determining equivalence?

假如想象 提交于 2019-12-22 04:38:21
问题 Java has a Comparator<T> for providing comparison of objects external to the class itself, to allow for multiple/alternate methods of doing ordered comparisons. But the only standard way of doing unordered comparisons is to override equals() within a class. What should I do when I want to provide multiple/alternate unordered comparisons external to a class? (Obvious use case is partitioning a collection into equivalence classes based on particular properties.) Assuming the end use is for

Config Spring cache using Guava

耗尽温柔 提交于 2019-12-22 04:01:32
问题 Following the spring documentation about cache I could use cache on my project, but how can I configure guava to define a expired time or size per cache name? applicationConfig.xml <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"/> Foo.java @Cacheable(value="courses", key="#user.id") public List<Course> getCoursesByUser(User user) { ... } 回答1: You can configure caches separately. See Spring Guava cache @Bean public CacheManager cacheManager() {

Extending List<T> in Java 8

对着背影说爱祢 提交于 2019-12-22 03:58:30
问题 I often want to map one list into another list. For example if I had a list of people, and I wanted a list of their names, I would like to do: GOAL List<Person> people = ... ; List<String> names = people.map(x -> x.getName()); Something like this is possible with Java 8: JAVA 8 VERSION List<String> names = people.stream() .map(x -> x.getName()) .collect(Collectors.toList()); But this is clearly not as nice. In fact, I think using Guava is cleaner: GUAVA VERSION List<String> names = Lists

guava-libraries: is Iterators.cycle() thread-safe?

给你一囗甜甜゛ 提交于 2019-12-22 03:54:13
问题 Suppose I have the following class: public class Foo { private List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5); private Iterator<Integer> iterator = Iterators.cycle(list); public void bar(){ Integer value = iterator.next(); doSomethingWithAnInteger(value); } } If an instance of Foo is acessed simultaneously by two threads, I need that each thread gets a different value from iterator.next() . Does the bar() method have to be made synchronized? Or is iterator.next() guaranteed to be

How can I convert MultiMap<Integer, Foo> to Map<Integer, Set<Foo>> using Guava?

牧云@^-^@ 提交于 2019-12-22 03:46:52
问题 I'm using MultiMap from Google Guava 12 like this: Multimap<Integer, OccupancyType> pkgPOP = HashMultimap.create(); after inserting values into this multimap, I need to return: Map<Integer, Set<OccupancyType>> However, when I do: return pkgPOP.asMap(); It returns me Map<Integer, Collection<OccupancyType>> How can I return Map<Integer, Set<OccupancyType>> instead ? 回答1: Look at this issue and comment #2 by Kevin Bourrillion, head Guava dev: You can double-cast the Map<K, Collection<V>> first