Google Core Libraries for Java 6+

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


But I'm getting an InstantiationException, and not a default instance.

Source: (StackOverflow)

Google Guava isNullOrEmpty for collections

I see that Guava has isNullOrEmpty utility method for Strings


Do we have anything similar for Lists? Something like


which should be equivalent to

list == null || list.isEmpty()

Also, do we have anything similar for Arrays? Something like


which should be equivalent to

arr == null || arr.length == 0

Source: (StackOverflow)

Maven for Eclipse 1.5.0 plugin cannot be installed under Kepler

I downloaded Eclipse Kepler and tried to install M2Eclipse from its update site.

After selecting Maven Integration for Eclipse, I clicked Next and got the following error:

Missing requirement: Maven Integration for Eclipse (org.eclipse.m2e.core requires 'bundle [14.0.1,16.0.0)' but it could not be found

So I searched through the internet to find out how to install the Guava Eclipse plugin. Some say it's from the Eclipse marketplace, but it cannot be downloaded. I downloaded the binary and tried to copy it to Eclipse's plugin directory. Still the same result.

cp ~/Downloads/guava-16.0.1.jar /Applications/eclipse/plugins/

How do I install the m2e plugin for Kepler?

Source: (StackOverflow)

Using Google Guava's Objects.ToStringHelper

I used ToStringBuilder.reflectionToString(class) in commons-lang, to implement toString() for simple DTOs. Now I'm trying to use Google Guava instead of Apache commons library. And I found Objects.ToStringHelper in Guava. But it's too verbose if there're lots of members in the class. For example:

public String toString() {
    return Objects.toStringHelper(this.getClass()).add("name", name)
            .add("emailAddress", emailAddress)
            .add("department", department).add("yearJoined", yearJoined)

is much simpler if I use commons-lang:

public String toString() {
    return ToStringBuilder.reflectionToString(this);

Is there any better ways to implement toString() with Guava, not with commons-lang?

Source: (StackOverflow)

Guava: Set + Function = Map?

Is there an idiomatic way to take a Set<K> and a Function<K,V>, and get a Map<K,V> live view? (i.e. the Map is backed by the Set and Function combo, and if e.g. an element is added to the Set, then the corresponding entry also exists in the Map).

(see e.g. Collections2.filter for more discussion on live views)

What if a live view is not needed? Is there something better than this:

public static <K,V> Map<K,V> newMapFrom(Set<K> keys, Function<? super K,V> f) {
    Map<K,V> map = Maps.newHashMap();
    for (K k : keys) {
        map.put(k, f.apply(k));
    return map;

Source: (StackOverflow)

Flattening an Iterable> in Guava

Is there a flatten method in Guava - or an easy way to convert an Iterable<Iterable<T>> to an Iterable<T>?

I have a Multimap<K, V> [sourceMultimap] and I want to return all values where the key matches some predicate [keyPredicate]. So at the moment I have:

Iterable<Collection<V>> vals = Maps.filterKeys(sourceMultimap.asMap(), keyPredicate).values();

Collection<V> retColl = ...;
for (Collection<V> vs : vals) retColl.addAll(vs);
return retColl;

I've looked through the Guava docs, but nothing jumped out. I am just checking I've not missed anything. Otherwise, I'll extract my three lines into a short flatten generic method and leave it as that.

Source: (StackOverflow)

Guava: how to combine filter and transform?

I have a collection of Strings, and I would like to convert it to a collection of strings were all empty or null Strings are removed and all others are trimmed.

I can do it in two steps:

final List<String> tokens =
    Lists.newArrayList(" some ", null, "stuff\t", "", " \nhere");
final Collection<String> filtered =
        Collections2.transform(tokens, new Function<String, String>(){

            // This is a substitute for StringUtils.stripToEmpty()
            // why doesn't Guava have stuff like that?
            public String apply(final String input){
                return input == null ? "" : input.trim();
        }), new Predicate<String>(){

            public boolean apply(final String input){
                return !Strings.isNullOrEmpty(input);

// Output, as desired: [some, stuff, here]

But is there a Guava way of combining the two actions into one step?

Source: (StackOverflow)

initializing a Guava ImmutableMap

Guava offers a nice shortcut way of initializing a map. However I get the following compiler error (Eclipse Indigo) when my map initializer gets to nine entries.

The method of(K, V, K, V, K, V, K, V, K, V) in the type ImmutableMap is not applicable for the arguments (String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String, String)

ImmutableMap<String,String> myMap = ImmutableMap.of(
        "key1", "value1", 
        "key2", "value2", 
        "key3", "value3", 
        "key4", "value4", 
        "key5", "value5", 
        "key6", "value6", 
        "key7", "value7", 
        "key8", "value8", 
        "key9", "value9"

The message appears to say that an ImmutableMap has a maximum size of four pairs of key,value. Obviously, this cannot be the case but I can't figure out what to do to increase the size of my initializer.

Can someone tell me what is missing?

Source: (StackOverflow)

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

I have a question about simplifying some Collection handling code, when using Google Collections (update: Guava).

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.newArrayList(Iterables.transform(matchingComputers, new Function<Computer, String>() {
    public String apply(Computer from) {
        return from.getResourceId();

Now, getResourceId() may return null (and changing that is not an option right now), yet in this case I'd like to omit nulls from the resulting String collection.

Here's one way to filter nulls out:

Collections2.filter(resourceIds, new Predicate<String>() {
    public boolean apply(String input) {
        return input != null;

You could put all that together like this:

Collection<String> resourceIds = Collections2.filter(
Lists.newArrayList(Iterables.transform(matchingComputers, new Function<Computer, String>() {
    public String apply(Computer from) {
        return from.getResourceId();
})), new Predicate<String>() {
    public boolean apply(String input) {
        return input != null;

But this is hardly elegant, let alone readable, for such a simple task! In fact, plain old Java code (with no fancy Predicate or Function stuff at all) would arguably be much cleaner:

Collection<String> resourceIds = Lists.newArrayList();
for (Computer computer : matchingComputers) {
    String resourceId = computer.getResourceId();
    if (resourceId != null) {

Using the above is certainly also an option, but out of curiosity (and desire to learn more of Google Collections), can you do the exact same thing in some shorter or more elegant way using Google Collections?

Source: (StackOverflow)

How to get max() element from List in Guava

Let's say we have a Collection of Items:

class Item {
    public String title;
    public int price;

List<Item> list = getListOfItems();

I would like to get an Item with a maximum price out of that list with Guava library (with Ordering, I presume). I mean something similar to this Groovy code:


How do I do that? How efficient is it?

Source: (StackOverflow)

how to transform List to Map with google collections?

I have a list with strings, and I have a functions to generate value for each key in the list, and I want to create map using a method. is there such function in google collections?

Source: (StackOverflow)

Interface/enum listing standard mime-type constants

I am looking among the standard libraries (like apache commons, jax, jboss, javax) for an interface or enum that lists the values of all the standard mime-type (aka content-type).

This interface should not be encumbered with too deep with other classes that would make it difficult to include the whole bunch as gwt source code.

for example

interface ContentType{
  String JSON = "Application/JSON";
  blah ... blah ...


enum ContentType{
  blah ... blah ...

Source: (StackOverflow)

Are there tutorials and resources explaining all components of guava-libraries?

I still precise that this request doesn't concern the google-collections part of the library which has a lot of resources: I'm speaking essentially about the services and the concurrency part.

I couldn't find tutorials regarding guava that aren't fully collections oriented. I know the collections are the most important part of the library, but others look interesting while they don't have much associated documentation.

Source: (StackOverflow)

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?

Source: (StackOverflow)

Combine multiple Collections into a single logical Collection?

Assume, I have a constant number of collections (e.g. 3 ArrayLists) as members of a class. Now, I want to expose all the elements to other classes so they can simply iterate over all elements (ideally, read only). I'm using guava collections and I wonder how I could use guava iterables/iterators to generate a logical view on the internal collections without making temporary copies.

Source: (StackOverflow)