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)

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)

How to shrink code - 65k method limit in dex

I have a rather large Android app that relies on many library projects. The Android compiler has a limitation of 65536 methods per .dex file and I am surpassing that number.

There are basically two paths you can choose (at least that I know of) when you hit the method limit.

1) Shrink your code

2) Build multiple dex files (see this blog post)

I looked into both and tried to find out what was causing my method count to go so high. The Google Drive API takes the biggest chunk with the Guava dependency at over 12,000. Total libs for Drive API v2 reach over 23,000!

My question I guess is, what do you think I should do? Should I remove Google Drive integration as a feature of my app? Is there a way to shrink the API down (yes, I use proguard)? Should I go the multiple dex route (which looks rather painful, especially dealing with third party APIs)?

Source: (StackOverflow)

Is it a good idea to use Google Guava library for Android development?

I am involved in the development of Android application which is a rather "thick" mobile client for a Web service. It heavily communicates with the server but also has a lot of inner logic too. So, I decided to use some features of Google Guava library to simplify development process. Here is a list of features I'm very interested in: immutable collections, base utils, collection extensions, functional programming sugar and idioms (common.collect and common.base), primitives utilities (common.primitives), hashing utilities (common.hash), concurrent utils (futures and AsyncFunction). Things I don't want to use in Android: common.cache (see question below), common.eventbus (we have better Android specific libs for this, such as Otto), (we can use okio for Android now).

I read that using Guava for Android can significantly slow down compilation process and also decrease the whole runtime performance: Bad performance with Guava Cache (in this case it is reasonable and there is no need to use Guava's cache for Android) and Adding Google Guava to Android project - significantly slows down the build

So, is it efficient to use Guava library in Android project or this library is designed to be used only for the server-side development, and I should go with standard solutions? Any explanations will be very appreciated.

Source: (StackOverflow)

Guava equivalent for IOUtils.toString(InputStream)

Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream to a String.

Since I am trying to move away from Apache Commons and to Guava: is there an equivalent in Guava? I looked at all classes in the package and I couldn't find anything nearly as simple.

Edit: I understand and appreciate the issues with charsets. It just so happens that I know that all my sources are in ASCII (yes, ASCII, not ANSI etc.), so in this case, encoding is not an issue for me.

Source: (StackOverflow)

Should I use Java8/Guava Optional for every method that may return null?

Optional is used to represent nullable object, Some uses of this class include

  1. As a method return type, as an alternative to returning null to
    indicate that no value was available
  2. To distinguish between "unknown" (for example, not present in a map) and "known to have no value" (present in the map, with value
  3. To wrap nullable references for storage in a collection that does not support null (though there are several other approaches to this that should be considered first)

For the first case, do I need to return Optional in all nullable return method?

Source: (StackOverflow)

The Guava library for java; what are its most useful and/or hidden features [closed]

I have had a quick scan of the guava api and the new collection types it provides(multimap and bimap for example appear useful) and I am thinking of including the library in the project(s) I work on. However, I also have a reticence to include libraries willy-nilly if they are of no great benefit and learning the features wastes valuable time.

Have you included the Guava library in your project and has it proved useful in any unexpected way ? Would you always use it in the future ? What has been its main benefit/time saver? What are its hidden features ?

Source: (StackOverflow)

How is Guava Splitter.onPattern(..).split() different from String.split(..)?

I recently harnessed the power of a look-ahead regular expression to split a String:


If printed to the console this expression returns:

[abc, 8]

Very pleased with this result, I wanted to transfer this to Guava for further development, which looked like this:


To my surprise the output changed to:



Source: (StackOverflow)

Predicate in Java

I am going through the code which uses Predicate in Java. I have never used predicate. Can someone guide me to any tutorial or conceptual explanation of predicate and their implementation in java ? Google didnt help much...

Source: (StackOverflow)