Guava: how to combine filter and transform?

In the upcoming latest version(12.0) of Guava, there will be a class named FluentIterable. This class provides the missing fluent API for this kind of stuff. Using FluentIterable, you should be able doing something like this: final Collection<String> filtered = FluentIterable .from(tokens) .transform(new Function<String, String>() { @Override public String apply(final String input) { return input … Read more

Why do ImmutableList.of() and friends prohibit null elements?

I explained this at the 25-minute point of this video: https://youtu.be/ZeO_J2OcHYM?t=1495 Sorry for the lazy answer, but this is after all only a “why” question (arguably not appropriate to StackOverflow?). EDIT: Here’s another point I’m not sure I made clear in the video: the total (across all of the world’s Java code), amount of extra … Read more

How to transform List to Map with Google collections?

Use Maps.uniqueIndex(Iterable, Function) : Returns an immutable map for which the Map.values() are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.(from javadoc) Example: Map<String,String> mappedRoles = Maps.uniqueIndex(yourList, new Function<String,String>() { public String apply(String from) { // do stuff here return result; … Read more

How to create a Multimap from a Map?

Assuming you have Map<String, Collection<String>> map = …; Multimap<String, String> multimap = ArrayListMultimap.create(); Then I believe this is the best you can do for (String key : map.keySet()) { multimap.putAll(key, map.get(key)); } or the more optimal, but harder to read for (Entry<String, Collection<String>> entry : map.entrySet()) { multimap.putAll(entry.getKey(), entry.getValue()); }

Java – Append quotes to strings in an array and join strings in an array

With Java 8+ Java 8 has Collectors.joining() and its overloads. It also has String.join. Using a Stream and a Collector The naive but effective way String wrapWithQuotesAndJoin(List<String> strings) { return strings.stream() .map(s -> “\”” + s + “\””) .collect(Collectors.joining(“, “)); } Shortest and probably better performing (somewhat hackish, though) String wrapWithQuotesAndJoin(List<String> strings) { return strings.stream() … Read more

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