Questions tagged [guava]

Google's Core Java Library for Java and Android development.

Guava

Guava is an open-source Java library that aims to make working in the Java language more pleasant and more productive.

It contains several core libraries that Google relies on in their Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, basic string processing, I/O, mathematics, etc.

Resources

3540 questions
52
votes
8 answers

How to create a Multimap from a Map>?

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> map = ImmutableMap.>of( "1",…
sly7_7
  • 11,961
  • 3
  • 40
  • 54
51
votes
5 answers

How to transform List to Map with Google collections?

I have a list of strings and I have a function to generate a value for each key in the list. I want to create a map using this function. Can I do this with Google collections?
oshai
  • 14,865
  • 26
  • 84
  • 140
50
votes
4 answers

Why do ImmutableList.of() and friends prohibit null elements?

Summary pretty much says it all. Here's the relevant snippet of code in ImmutableList.createFromIterable(): if (element == null) { throw new NullPointerException("at index " + index); } I've run into this several times and can't see why a…
Matt McHenry
  • 20,009
  • 8
  • 65
  • 64
49
votes
6 answers

How to transform List to another List

I have two lists of objects; List and List. X and Y are ojects that look like: public class X { String a; String b; String v; String w; String m; String n; } public class Y { String a; String b; List
zhm
  • 491
  • 1
  • 4
  • 3
47
votes
1 answer

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 tokens = Lists.newArrayList(" some ",…
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
45
votes
2 answers

Why there is no getFirst(iterable) method?

Iterables present two methods for getLast public static T getLast(Iterable iterable); public static T getLast(Iterable iterable, @Nullable T defaultValue); but only one for getFirst public static T getFirst(Iterable
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
45
votes
4 answers

What's the difference between Collections.unmodifiableSet() and ImmutableSet of Guava?

JavaDoc of ImmutableSet says: Unlike Collections.unmodifiableSet, which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public…
卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
45
votes
6 answers

Compact way to create Guava Multimaps?

If I want to create a new Multimap with simple defaults, I curently need to do something like: private final Multimap providersToClasses = Multimaps .newListMultimap( new HashMap>(), …
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
44
votes
4 answers

How to automatically refresh Cache using Google Guava?

I am using Google Guava library for caching. For automatic cache refresh we can do as follows: cache = CacheBuilder.newBuilder() .refreshAfterWrite(15, TimeUnit.MINUTES) .maximumSize(100) …
K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
43
votes
7 answers

What is an efficient and elegant way to add a single element to an immutable set?

I have an immutable set (cast as a Set) that potentially contains many elements. I need a Collection that contains the elements from that set plus one additional element. I have kludgy code in place to copy the set, then append the element,…
Max
  • 11,377
  • 5
  • 24
  • 15
42
votes
6 answers

Atomically incrementing counters stored in ConcurrentHashMap

I would like to collect some metrics from various places in a web app. To keep it simple, all these will be counters and therefore the only modifier operation is to increment them by 1. The increments will be concurrent and often. The reads…
wishihadabettername
  • 14,231
  • 21
  • 68
  • 85
41
votes
2 answers

Flattening an Iterable> in Guava

Is there a flatten method in Guava - or an easy way to convert an Iterable> to an Iterable? I have a Multimap [sourceMultimap] and I want to return all values where the key matches some predicate [keyPredicate]. So at the moment…
Andy Whitfield
  • 2,373
  • 2
  • 19
  • 22
41
votes
3 answers

Why does Guava's ImmutableList have so many overloaded of() methods?

I was just looking at Guava's ImmutableList and I noticed that the of() method was overloaded 12 times. It looks to me that all they needed was: static ImmutableList of(); static ImmutableList of(E element); // not even…
jjnguy
  • 136,852
  • 53
  • 295
  • 323
41
votes
3 answers

Guava libraries and GWT

Just discovered the Guava libraries project. Do these work with GWT?
jldupont
  • 93,734
  • 56
  • 203
  • 318
41
votes
1 answer

using guava cache without a load function

My java app has a cache, and I'd like to swap out the current cache implementation and replace it with the guava cache. Unfortunately, my app's cache usage doesn't seem to match the way that guava's caches seem to work. All I want is to be able to…
Mike W
  • 1,128
  • 1
  • 13
  • 27