Skip to content
Advertisement

Tag: guava

What is the equivalent way to do Ordering.lexicographical() in Java 8 Comparator?

Is there a way to implement Ordering.lexicographical() with Java 8 Comparator? Comparator.thenCompare seems to be limited in this Answer Seemingly not, no. As a result, Guava still provides this functionality, but in the new class Comparators: https://guava.dev/releases/snapshot/api/docs/com/google/common/collect/Comparators.html#lexicographical(java.util.Comparator). Note that Guava generally does a good job telling you whether and how to migrate off of it to new Java features, so

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 is that only possible with Java’s GZIPInputStream/ZipInputStream? Answer You can create your own

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 domain name may not exceed the length of 253 characters in its textual representation. In the internal binary

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 the most frequent element(s) occur, and then scan

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/releases/19.0/api/docs/com/google/common/collect/Range.html Of course, there will

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: I don’t specify an expiry time. The problem is that according to the values of the key, some associated values may expire and others may not. And CacheLoader

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 has the right semantics: Answer This is already implemented in

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 the signature of

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 some

Guava cache and preserving checked exceptions

I’m refactoring some code to use guava Cache. Initial code: In order not to break something I need to preserve any thrown exception as is, without wrapping it. Current solution appears somewhat ugly: Is there any possible way to make it nicer? Answer Just after writing the question started thinking about utility method powered with generics. Then remembered something about

Advertisement