google-collections

Create Weak Multimap with Google Collections

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? ...

Would this cause Garbage Collection issues

I wrote a little Linq like DSL on top of Google Collections public class IterableQuery { public static <T> Where<T> from(Iterable<T> originalCollection) { return new Where<T>( Iterables.transform(originalCollection, IterableQuery.<T>SAME())); } private static <T> Function<T, T> SAME() { return new Function<T...

Cons'ing a List in Java

Say I have a java.util.List list and I want to create a new List by adding an element e to the beginning of list (i.e., I want to cons e and list). For example, if list is [1,2,3,4] and e is 5, then cons(e,list) will be [5,1,2,3,4] It's OK for the elements of list and cons(e,list) to be shared, but list should not be modified. Wha...

Generic Test harness for java.util.Map?

I have a custom implementation of the Map interface which does some fancy stuff, like lazy evaluation of functions. the implementation should appear immutable after construction from outside (e.g. no put() and putAll() methods are supported) I it looks like it works in the most basic conditions. Since it is quite complex, i am sure ther...

Map implementation with duplicate keys

I want to have Map with duplicate keys, I know there are many Map implementations(eclipse shows me about 50), so I bet there must be one that allows this. I know its easy to write your own Map that does this, but i would rather use some existing solution. Maybe something in commons-collections or google-collections? ...

Why no SortedMultiset in Google Collections?

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 speci...

Apache Commons vs. Google Collections

I was looking for a bidirectional map implementation in Java, and stumbled upon these two libraries: Apache Commons Collections Google Collections Both are free, have the bidirectional map implementation that I was looking for (BidiMap in Apache, BiMap in Google), are amazingly nearly the same size (Apache 493 kB, Google 499 kB) and ...

Java: Instantiate Google Collection's HashBiMap

I'm using Eclipse, and I've added google-collect.1.0-rc2.jar as a referenced library. Yet somehow this still doesn't work: import com.google.common.collect.HashBiMap; public class Odp { //... HashBiMap<Character, Integer> charOcc = HashBiMap<Character, Integer>.create(); } Eclipse gives the following erro...

Google MultiMap and synchronization

Hi, I dont understand why i get a concurrentModificationException when i iterate trough this multimap. I read the following javadoc entry, but i'm not sure to understand the whole thing. I tried to add synchronized block. But i doubt on what to synchronize with, and when. Dont hesite to be crude if do huge mystake with concurency ;-)...

Limit a ListIterator to the first N elements (optimized)

What is a simple and fast way to get an iterator that returns at most N elements from the start of a List? The simplest versions I could come up with are: #1: import com.google.common.collect.Iterators; // ... public static <E> Iterator<E> lengthLimitedIterator(Iterable<E> source, int maxLen) { return Iterators.partition(source....

TreeMultimap in Google Collections

Has anybody used TreeMultimap in Google Collections? I understand that with a TreeMultimap, its keys and values are ordered by their natural ordering or by supplied comparators. I was wondering if there is a function that allows user to supply a key and returns all the values whose keys are greater than the user-supplied key. This can be...

Is there an elegant way to remove nulls while transforming a Collection using Google Collections?

I have a question about simplifying some Collection handling code, when using Google Collections. I've got a bunch of "Computer" objects, and I want to end up with a Collection of their "resource id"s. This is done like so: Collection<Computer> matchingComputers = findComputers(); Collection<String> resourceIds = Lists.newArrayLi...

Java: null pointer exception?

This code is causing a null pointer exception. I have no idea why: private void setSiblings(PhylogenyTree node, Color color) throws InvalidCellNumberException { PhylogenyTree parent = node.getParent(); for (PhylogenyTree sibling : parent.getChildren()) { if (! sibling.equals(node)) { Animal animal = sibling....

Java, Google Collections Library; problem with AbstractIterator?

I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I've reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable. Essentially in the following code, the uncom...

Google Collections equivalent to Apache Commons Collections ArrayUtils.toObject and ArrayUtils.toPrimitive

Since everyone praises Google Collections (e.g. in here) How come I can't find the equivalent of ArrayUtils.toObject() and ArrayUtils.toPrimitive()? is it that unusable? did I miss it? ...

Does Google-Collections work with GWT?

I was just reading my news when I stumbled on Google-Collections project. Does this work with GWT? ...

Guava: ImmutableSet of either values or an empty set

I need a cleaner solution to using an ImmutableSet. I have code that looks like Set foo = ImmutableSet.copyOf(aGeoR.getFailed()); it works great when aGeoR.getFailed() returns one or more entries. it fails when the returned set is null. When its null, I need a Set foo = ImmutableSet.of(); What is the clean way to do this? ...

Has Google Collections 1.0 been mavenized?

According to the entry at MVNRepository, at the time of this writing, the latest version in maven central is 1.0-rc5. I am aware there are no changes between 1.0 which was recently announced and RC5, but I guess the nit picker in me wants to find a 1.0 version. Here's the POM entry for RC5. <dependency> <groupId>com.google.collect...

MapMaker and ReferenceMap - Google Collections

Hi, I understand referenceMap from the alpha version of Google Collections has been replaced by MapMaker. I used this ReferenceMap constructor with the backing map: public ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType, ConcurrentMap backingMap) { this(keyReferenceType, valueReferenceType, back...

java.lang.NoClassDefFoundError: com/google/common/base/internal/Finalizer$ShutDown (wrong name: com/google/common/base/internal/Finalizer)

Our application uses the MapMaker class from Google collections, and we're getting the exception below, but only on OS X 10.4 using webstart. It works fine when launched from an app bundle, and on OS X 10.5 and Windows. This has started happening since our upgrade from RC2 to RC5 (we skipped all the intervening versions). We've since ...