Skip to content

Tag: guava

Guava Resources.readLines() for Zip/Gzip files

I’ve found the Resources.readLines() and Files.readLines() to be helpfull in simplifiying my code. The problem is that I often read gzip-compressed txt-files or txt-files in zip archives from URL’s (HTTP and FTP). Is there a way to use Guava’s methods to read from these URL’s too? Or i…

Maximum length of domain name

As I noticed class from google library com.google.common.net.InternetDomainName containts the following constant: Code below, checks length during creation of InternetDomainName instance: But RFC-2181 says that: So, what is valid max length for domain name? Answer This is straight from wikipedia: The full dom…

Java-get most common element in a list

Does Java or Guava have something that will return most common element in a list? [1,3,4,3,4,3,2,3,3,3,3,3] return 3 Answer This is fairly easy to implement yourself: 3 If you want to handle cases where there’s more then one most frequent element, you can scan the list once to determine how many times t…

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? Answer Using Guava, you can resort to a Range: https://guava.dev/…

implementing a lazy Supplier in java

What is the right paradigm or utility class (can’t seem to find a preexisting class) to implement a lazy supplier in Java? I want to have something that handles the compute-once/cache-later behavior and allows me to specify the computation behavior independently. I know this probably has an error but it…

Library method to partition a collection by a predicate

I have a collection of objects that I would like to partition into two collections, one of which passes a predicate and one of which fails a predicate. I was hoping there would be a Guava method to do this, but the closest they come is filter, which doesn’t give me the other collection. I would image th…

How to use replaceValues method of Guava Multimap?

I want to add, remove and replace values in a MultiMap provided by Guava. I do this currently to add values.. Removing values is easier with Guava library. But how can I use the replaceValues method? I mean this Say I wanted to replace value 4.3 with a new value 5.99, how should I do that, the method expects …