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

ListenableFuture, FutureCallback and timeouts

Based on the examples of guava I've seen I've been looking for elegant solutions to my problem. Specifically, I like the way Futures.addCallback(ListenableFuture, FutureCallback) works, but I'd like to be able to set a timeout on the length of time…
Nick Campion
  • 10,479
  • 3
  • 44
  • 58
34
votes
6 answers

Remove duplicates from List using Guava

How can we remove duplicates from List with the help of Guava api? Currently I am following this: private List removeDuplicate(List list){ return new ArrayList(new LinkedHashSet(list)); }
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
33
votes
6 answers

How to convert Map to Map ? (option : using guava)

I have a Map the String key is nothing but numeric value like "123" etc. I'm getting numeric value because this values are coming from the UI in my JSF component. I don't want to change the contract of UI component. Now I would like…
Premraj
  • 7,802
  • 8
  • 45
  • 66
33
votes
6 answers

Guava: Set + Function = Map?

Is there an idiomatic way to take a Set and a Function, and get a Map 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…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
33
votes
6 answers

Java 8 Stream String Null Or Empty Filter

I've got Google Guava inside Stream: this.map.entrySet().stream() .filter(entity -> !Strings.isNullOrEmpty(entity.getValue())) .map(obj -> String.format("%s=%s", obj.getKey(), obj.getValue())) .collect(Collectors.joining(",")) As you see there is a…
ServerSideCat
  • 1,992
  • 3
  • 18
  • 24
33
votes
5 answers

no such method error: ImmutableList.copyOf()

I'm using Guava-05-snapshot, with Sun's JDK 1.6 The code blows up executing this snippet: List badpasswords = Lists.newArrayList( Password.badWords); Collections.sort(badpasswords); ImmutableList tmp =…
fishtoprecords
  • 2,394
  • 7
  • 27
  • 38
33
votes
2 answers

Is there any generic version of toArray() in Guava or Apache Commons Collections?

What I'm looking for is a generic version of Object[] java.util.Collection.toArray() or a less verbose alternative to using T[] java.util.Collection.toArray(T[] array). I can currently write: Collection strings; String[] array =…
Mihai
  • 1,261
  • 2
  • 12
  • 19
32
votes
5 answers

ImmutableMap.of() workaround for HashMap in Maps?

There are utility methods to create ImmutableMap like Immutable.of(Key, value) and its overload. But such methods don't exist for HashMap or LinkedHashMap in Maps class. Is there any better way to do this or Guava assumes such a map is always a…
Premraj
  • 7,802
  • 8
  • 45
  • 66
32
votes
4 answers

Simplest way to iterate through a Multiset in the order of element frequency?

Consider this example which prints out some device type stats. ("DeviceType" is an enum with a dozenish values.) Multiset histogram = getDeviceStats(); for (DeviceType type : histogram.elementSet()) { System.out.println(type + ": " +…
Jonik
  • 80,077
  • 70
  • 264
  • 372
32
votes
2 answers

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: "abc8".split("(?=\\d)|\\W") If printed to the console this expression returns: [abc, 8] Very pleased with this result, I wanted to transfer this to Guava for…
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
31
votes
4 answers

Google guava vs Scala collection framework comparison

There are a lot of common concepts: immutable collection, collection view, strict/non strict collection, collection builders same patterns in Guava and Scala Collection API. So what is the difference? Is both library are consistent with…
yura
  • 14,489
  • 21
  • 77
  • 126
31
votes
1 answer

When to use an Event Bus?

I'm designing the backend for a new Java web app and am trying to decide whether or not to use an Event Bus; specifically the Guava EventBus. Most server-side requests will be synchronous: that is, the user is requesting data and needs a response…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
31
votes
7 answers

Designing a Guava LoadingCache with variable entry expiry

I am using Guava's LoadingCache into my project to handle thread-{safe,friendly} cache loading and it works wonderfully well. However, there is a limitation. The current code defining the cache looks like this: cache =…
fge
  • 119,121
  • 33
  • 254
  • 329
31
votes
3 answers

Is there a corresponding immutable enumMap in guava?

I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap to EnumMap. However, I'd also like the immutable property offered by ImmutableMap. Is there a variant,…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
30
votes
4 answers

How to iterate through google multimap

I have to iterate through google multimap. But I am using jdk 1.4 and can't switch to higher version. So i can not use generic features. My multimap can have multiple values for a key. There might be a situation when a value of multimap is multimap…
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90