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
70
votes
1 answer

What is the best way to cache single object within fixed timeout?

Google Guava has CacheBuilder that allows to create ConcurrentHash with expiring keys that allow to remove entries after the fixed tiemout. However I need to cache only one instance of certain type. What is the best way to cache single object within…
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
67
votes
9 answers

Gradle: Override transitive dependency by version classifier

One of the dependencies declared in my project has a transitive dependency on 'com.google.guava:guava:15.0'. But my application deployed on WAS/Weblogic doesn't work due to a CDI issue which has been fixed in 'com.google.guava:guava:15.0:cdi1.0'.…
dinup24
  • 1,652
  • 3
  • 16
  • 26
63
votes
4 answers

How to solve 'Program type already present: com.google.common.util.concurrent.ListenableFuture'?

I am trying to use WorkManager 1.0.0-alpha09. and getting this error: Program type already present: com.google.common.util.concurrent.ListenableFuture Message{kind=ERROR, text=Program type already present:…
Simran Marok
  • 707
  • 1
  • 5
  • 9
62
votes
5 answers

What is the difference between google's ImmutableList and Collections.unmodifiableList ()?

From ImmutableList javadocs: Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change.…
Roman
  • 64,384
  • 92
  • 238
  • 332
62
votes
3 answers

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 As a method return type, as an alternative to returning null to indicate that no value was available To distinguish between "unknown" (for example, not present in a…
lessisawesome
  • 1,313
  • 1
  • 12
  • 14
60
votes
5 answers

How to avoid caching when values are null?

I am using Guava to cache hot data. When the data does not exist in the cache, I have to get it from database: public final static LoadingCache UID2UCache = CacheBuilder.newBuilder() //.maximumSize(2000) .weakKeys() …
Shisoft
  • 4,197
  • 7
  • 44
  • 61
59
votes
10 answers

How to convert a possible null-value to a default value using Guava?

Does Guava provide a method to get a default value if a passed object reference is null ? I'am looking for something like T nullToDefault(T obj, T default), were the default is returned if obj is null. Here on stackoverflow I found nothing about…
Chriss
  • 5,157
  • 7
  • 41
  • 75
59
votes
5 answers

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…
Jonik
  • 80,077
  • 70
  • 264
  • 372
58
votes
1 answer

Concatenate string values with delimiter handling null and empty strings?

In Java 8 I have some number of String values and I want to end up with a comma delimited list of valid values. If a String is null or empty I want to ignore it. I know this seems common and is a lot like this old question; however, that…
eze
  • 2,332
  • 3
  • 19
  • 30
58
votes
7 answers

Is there java.util.concurrent equivalent for WeakHashMap?

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency? Collections.synchronizedMap(new WeakHashMap()); i.e. is there something from java.util.concurrent one…
Nikita
  • 6,019
  • 8
  • 45
  • 54
57
votes
9 answers

Getting default value for 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…
ripper234
  • 222,824
  • 274
  • 634
  • 905
56
votes
6 answers

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…
philipjkim
  • 3,999
  • 7
  • 35
  • 48
54
votes
9 answers

Sort List in reverse in order

I have List list1 in direct order. List list = Ordering.natural().sortedCopy(asu2); How to change order. And I don't know how to rewrite methods from extends class, please write with examples or speak clearly.
Eldar Nezametdinov
  • 1,917
  • 4
  • 21
  • 23
54
votes
4 answers

Populating a List with a contiguous range of integers

I'd like to have a list which contains the integers in the range 1 to 500. Is there some way to create this list using Guava (or just plain Java) without having to loop through the range and add the values individually within my own code?
user1596371
53
votes
7 answers

Java - Append quotes to strings in an array and join strings in an array

I would like to append double quotes to strings in an array and then later join them as a single string (retaining the quotes). Is there any String library which does this? I have tried Apache commons StringUtils.join and the Joiner class in Google…
Anand
  • 1,791
  • 5
  • 23
  • 41