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
110
votes
9 answers

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…
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
103
votes
4 answers

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 its implementation in Java?
srikanth
  • 1,211
  • 2
  • 9
  • 11
98
votes
7 answers

How to make a new list with a property of an object which is in another list

Imagine that I have a list of certain objects: List And I need to generate another list including the ids of Students in the above list: List Avoiding using a loop, is it possible to achieve this by using apache collections or…
javatar
  • 4,542
  • 14
  • 50
  • 67
95
votes
6 answers

is guava-libraries available in maven repo?

I am looking to find guava-libraries in maven repository. It looks like guava is adding more features to google-collections library.
mjlee
  • 3,374
  • 4
  • 27
  • 22
92
votes
4 answers

What's the point of Guava's Optional class

I've recently read about this and seen people using this class, but in pretty much all cases, using null would've worked as well - if not more intuitively. Can someone provide a concrete example where Optional would achieve something that null…
RAY
  • 6,810
  • 6
  • 40
  • 67
91
votes
12 answers

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)…
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
89
votes
8 answers

How to JUnit test that two List contain the same elements in the same order?

Context I am writing a simple JUnit test for the MyObject class. A MyObject can be created from a static factory method that takes a varargs of String. MyObject.ofComponents("Uno", "Dos", "Tres"); At any time during the existence of MyObject,…
Robottinosino
  • 10,384
  • 17
  • 59
  • 97
87
votes
5 answers

How can I collect a Java 8 stream into a Guava ImmutableCollection?

I would like to do the following: List list = IntStream.range(0, 7).collect(Collectors.toList()); but in a way that the resulting list is an implementation of Guava's ImmutableList. I know I could do List list = IntStream.range(0,…
Zoltán
  • 21,321
  • 14
  • 93
  • 134
87
votes
2 answers

Cleanest way to create a Guava Multimap from a Java 8 stream

I have a List and want a Multimap where we've grouped the Foo's by their getId() function. I am using Java 8 and its almost awesome in that you can do: List foos = ... Map> foosById =…
Scott B
  • 1,665
  • 1
  • 15
  • 14
86
votes
5 answers

Guava: Why is there no Lists.filter() function?

Is there a reason there's Lists.transform() but no Lists.filter() ? How do I filter a list correctly? I could use new ArrayList(Collection2.filter()) of course, but this way it's not guaranteed that my ordering stays the same, if I understand…
Fabian Zeindl
  • 5,860
  • 8
  • 54
  • 78
81
votes
8 answers

Lists.newArrayList vs new ArrayList

What is the best construction for creating a List of Strings? Is it Lists.newArrayList() (from guava) or new ArrayList()? is it just a personal preference? or is it just Type generic type inference? or is there any theoretical or practical value in…
asela38
  • 4,546
  • 6
  • 25
  • 31
80
votes
10 answers

A Queue that ensure uniqueness of the elements?

I'm looking for a implementation of java.util.Queue or something in the Google collection who behave like a Queue, but also ensure that each element of the queue is unique. (all further insertion will have no effect) It's that possible, or will I…
Antoine Claval
  • 4,923
  • 7
  • 40
  • 68
75
votes
3 answers

What's the point of Guava checkNotNull

I'm pretty new to Guava (let's be honest, I'm not "pretty new", I'm a complete rookie on that subject) and so I decided to go through some documentation and got quite amazed while reading…
Ar3s
  • 2,237
  • 2
  • 25
  • 47
72
votes
9 answers

Managing highly repetitive code and documentation in Java

Highly repetitive code is generally a bad thing, and there are design patterns that can help minimize this. However, sometimes it's simply inevitable due to the constraints of the language itself. Take the following example from…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
71
votes
6 answers

Error:com.android.builder.dexing.DexArchiveBuilderException: Failed to process guava-21.0.jar Android 3.1 Dev Channel

After updating my dependency versions in AndroidStudio3.1, I started getting the following error: Information:Gradle tasks [:app:assembleDebug] Error:com.android.builder.dexing.DexArchiveBuilderException: Failed to process…
Atak
  • 897
  • 2
  • 10
  • 17