guava

Soft/Weak key MapMAker with equals for key

Dear all, I need a Concurrent Hash Map with Weak or Soft keys were the equality is equals and not ==. For this kind of keys, google collection chooses == by default. Is there a way to override this choice? How should I proceed? Best regards, Nicolas. ...

Is Guava's ImmutableList.Builder thread safe?

What are the thread safety guarantees for Guava's ImmutableList.Builder? The javadocs don't say. ...

Getting default value for java primitive types

I have a java primitive type at hand: Class c = int.class; // or long.class, or boolean.class I'd like to get a 'default value' for this class - specifically the value is assigned to fields of this type if they are not initialized. E.g., '0' for a number, 'false' for a boolean. Is there a generic way to do this? I tried c.newInstanc...

How do the versions of Guava work?

I would like to use Guava in a project, but my PM doesn't like the "r05" suffix, saying that it looks like it's not stable. In fact, the part I need is only the Google-Collections 1.0 which is now deprecated (my PM doesn't like that word either). So I don't really get the versioning of Guava/Google-Collections. I'm currently doing the ...

Direct comparator in Java out of the box

I have a method which needs a Comparator for one of its parameters. I would like to pass a Comparator which does a normal comparison and a reverse comparator which does in reverse. java.util.Collections provides a reverseOrder() this is good for the reverse comparison, but I could not find any normal Comparator. The only solution what ...

Where can I find PrimitiveArrays in Guava?

I was using com.google.common.collect.PrimitiveArrays from Google Collections, however I cannot find it in Guava, was it renamed? Where can I find it? ...

Find top N elements in a Multiset from Google Collections?

A Google Collections Multiset is a set of elements each of which has a count (i.e. may be present multiple times). I can't tell you how many times I want to do the following Make a histogram (exactly Multiset) Get the top N elements by count from the histogram Examples: top 10 URLs (by # times mentioned), top 10 tags (by # times app...

Why doesn't Google's Multimap's entries() method return Key/Collection pairs?

I would like to be able to retrieve from my com.google.collections.Multimap<A, B> a Collection<Entry<A, Collection<B>>> which I expected from the entries() method, but in fact it returns a Collection<Entry<A, B>>. Is there a method which does what I want? Currently I'm iterating like this: for (A key: mmap.keySet()) { Collection<B...

How to create a Multimap<K,V> from a Map<K, Collection<V>> ?

I didn't find such a multimap construction... When i want to do this, I iterate over the map, and populate the multimap. Is there an other way ?? final Map<String, Collection<String>> map = ImmutableMap.<String, Collection<String>>of( "1", Arrays.asList("a", "b", "c", "c")); System.out.println(Multimaps.forMap(map)); final ...

Why does Iterables.find() in Guava throw NoSuchElementException, instead of returning null?

I love Google Guava and use it a lot, but there is one method I always find me writing.. public static <T> T tryFind(Iterable<T> iterable, Predicate<T> predicate){ for(T t : iterable){ if(predicate.apply(t)){ return t; } } return null; } To me this seems to be a very useful addition to...

List Sorting puzzle

Assuming I have final Iterable<String> unsorted = asList("FOO", "BAR", "PREFA", "ZOO", "PREFZ", "PREFOO"); What can I do to transform this unsorted list into this: [PREFZ, PREFA, BAR, FOO, PREFOO, ZOO] (a list which begin with known values that must appears first (here "PREFA" and "PREFZ") and the rest is alphabetically sorted) ...

Splitter blows up on simple Pattern

I am just starting to us Guava in place of Google-Collections. The Splitter class seemed cool. But when I use it, like this: private static final Pattern p = Pattern.compile(" +"); private static final Splitter usSplitter = Splitter.on(p).trimResults(); I get a stack dump: java.lang.NoSuchMethodError: com.google.common.base.Platform....

no such method error: ImmutableList.copyOf()

I'm using Guava-05-snapshot, with Sun's JDK 1.6 The code blows up executing this snippet: List<String> badpasswords = Lists.newArrayList( Password.badWords); Collections.sort(badpasswords); ImmutableList<String> tmp = ImmutableList.copyOf(badpasswords); Specifically on the ImmutableList.copyOf() call. This code has worked for months, ...

What is the proper error message to supply to Google Guava's Preconditions.* methods?

For example when using Preconditions.checkArgument, is the error message supposed to reflect the passing case or the failing case of the check in question? import static com.google.common.base.Preconditions.*; void doStuff(int a, int b) { checkArgument(a == b, "a == b"); // OR checkArgument(a == b, "a != b"); } ...

omitEmptyStrings cannot be worked properly

Hi!, gurus I'm stuck in the middle. I'm creating wrapper class for com.google.common.base.Splitter. But the method omitEmptyStrings doesn't work properly. Please, give your awesome guidances to me. Here is [the caller] public class Hoge { public static void main(String[] args) { Iterable<String> hoge = Splitter.on(...

Open InputStream as Reader

Can I easily convert InputStream to BufferedReader using Guava? I'm looking for something like: InputStream inputStream = ...; BufferedReader br = Streams.newBufferedReader(inputStream); I can open files using the Files.newReader(File file, Charset charset). That's cool and I want to do the same using the InputStream. UPDATE: Using...

how to delete elements from a transformed collection using a predicate?

If I have an ArrayList<Double> dblList and a Predicate<Double> IS_EVEN I am able to remov e all even elements from dblList using: Collections2.filter(dblList, IS_EVEN).clear() if dblList however is a result of a transformation like dblList = Lists.transform(intList, TO_DOUBLE) this does not work any more as the transformed list is...

Computing map: computing value ahead of time

I have a computing map (with soft values) that I am using to cache the results of an expensive computation. Now I have a situation where I know that a particular key is likely to be looked up within the next few seconds. That key is also more expensive to compute than most. I would like to compute the value in advance, in a minimum-pr...

Using MapMaker to create a cache

Hello, I want to use MapMaker to create a map that caches large objects, which should be removed from the cache if there is not enough memory. This little demo program seems to work fine: public class TestValue { private final int id; private final int[] data = new int[100000]; public TestValue(int id) { this.id = ...

Google Guava and an error I can't figure out

I get this error when trying to compile: The method filter(Iterable<T>, Predicate<? super T>) in the type Iterables is not applicable for the arguments (Iterator<PeopleSoftBalance>, ColumnLikePredicate<PeopleSoftBalance>) Here is the ColumnLikePredicate class sig: public class ColumnLikePredicate<T extends RowModel> implements Pre...