What’s the point of Guava’s Optional class

Guava team member here. Probably the single biggest disadvantage of null is that it’s not obvious what it should mean in any given context: it doesn’t have an illustrative name. It’s not always obvious that null means “no value for this parameter” — heck, as a return value, sometimes it means “error”, or even “success” … Read more

is guava-libraries available in maven repo?

Starting from r03, Guava releases may be found in the central Maven repository. You may include Guava by adding the following dependency to your POM: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> Note: the above version number may be outdated when you read this. To know the latest available version, you may look here Version updated on … Read more

Predicate in Java

I’m assuming you’re talking about com.google.common.base.Predicate<T> from Guava. From the API: Determines a true or false value for a given input. For example, a RegexPredicate might implement Predicate<String>, and return true for any string that matches its given regular expression. This is essentially an OOP abstraction for a boolean test. For example, you may have … Read more

Get empty string when null

You can use Objects.toString() (standard in Java 7): Objects.toString(gearBox, “”) Objects.toString(id, “”) From the linked documentation: public static String toString(Object o, String nullDefault) Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise. Parameters: o – an object nullDefault – string to … Read more

Guava equivalent for IOUtils.toString(InputStream)

You stated in your comment on Calum’s answer that you were going to use CharStreams.toString(new InputStreamReader(supplier.get(), Charsets.UTF_8)) This code is problematic because the overload CharStreams.toString(Readable) states: Does not close the Readable. This means that your InputStreamReader, and by extension the InputStream returned by supplier.get(), will not be closed after this code completes. If, on the … Read more

Google Guava isNullOrEmpty for collections

No, this method does not exist in Guava and is in fact in our “idea graveyard.” We don’t believe that “is null or empty” is a question you ever really want to be asking about a collection. If a collection might be null, and null should be treated the same as empty, then get all … Read more

UnmodifiableMap (Java Collections) vs ImmutableMap (Google) [duplicate]

An unmodifiable map may still change. It is only a view on a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view: Map<String, String> realMap = new HashMap<String, String>(); realMap.put(“A”, “B”); Map<String, … Read more

Combine multiple Collections into a single logical Collection?

With Guava, you can use Iterables.concat(Iterable<T> …), it creates a live view of all the iterables, concatenated into one (if you change the iterables, the concatenated version also changes). Then wrap the concatenated iterable with Iterables.unmodifiableIterable(Iterable<T>) (I hadn’t seen the read-only requirement earlier). From the Iterables.concat( .. ) JavaDocs: Combines multiple iterables into a single … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)