Google-guava checkNotNull and IntelliJ IDEA’s “may produce java.lang.NullPointerException”

Through a combination of @Contract annotations and the External Annotations feature, you can now annotate Preconditions methods such that IntelliJ applies the correct static analysis to calls to these methods. Let’s say we have this example public void doSomething(Object someArg) { Preconditions.checkArgument(someArg != null); someArg.doSomethingElse(); //currently gives NPE warning if (someArg != null) { //no … Read more

Google Guava Supplier Example

The Supplier interface is simply an abstraction of a no-arg function that returns a value… it is a means of getting some instance or instances of an object. Since it is so general, it can be used as many things. Jared explained how the Multimaps factories utilize it as a factory for creating a new … Read more

Caffeine versus Guava cache

The main difference is because Caffeine uses ring buffers to record & replay events, whereas Guava uses ConcurrentLinkedQueue. The intent was always to migrate Guava over and it made sense to start simpler, but unfortunately there was never interest in accepting those changes. The ring buffer approach avoids allocation, is bounded (lossy), and cheaper to … Read more

Grouping elements of a list into sublists (maybe by using guava)

Sure it is possible, and even easier with Guava 🙂 Use Multimaps.index(Iterable, Function): ImmutableListMultimap<E, E> indexed = Multimaps.index(list, groupFunction); If you give concrete use case it would be easier to show it in action. Example from docs: List<String> badGuys = Arrays.asList(“Inky”, “Blinky”, “Pinky”, “Pinky”, “Clyde”); Function<String, Integer> stringLengthFunction = …; Multimap<Integer, String> index = Multimaps.index(badGuys, … Read more

How to get max() element from List in Guava

Ordering<Item> o = new Ordering<Item>() { @Override public int compare(Item left, Item right) { return Ints.compare(left.price, right.price); } }; return o.max(list); It’s as efficient as it can be: it iterates through the items of the list, and returns the first of the Items having the maximum price: O(n).

ProGuard configuration for Guava with obfuscation and optimization

As of Guava 17.0, this is what I needed in ProGuard config: -dontwarn javax.annotation.** -dontwarn javax.inject.** -dontwarn sun.misc.Unsafe Otherwise build fails with warnings like: Warning: com.google.common.base.Absent: can’t find referenced class javax.annotation.Nullable (That’s because Guava uses annotations that are not part of Android runtime (android.jar). In this case it’s fine to just mute the warnings.) If … Read more

Guava ImmutableMap has noticeably slower access than HashMap

As Louis Wasserman said, ImmutableMap is not optimized for objects with slow equals method. I think the main difference is here: HashMap: if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; ImmtubleMap: if (key.equals(candidateKey)) { return entry.getValue(); As you can see, to check for collisions, HashMap first check the hashes. … Read more

Does Java 8 have cached support for suppliers?

There’s no built-in Java function for memoization, though it’s not very hard to implement it, for example, like this: public static <T> Supplier<T> memoize(Supplier<T> delegate) { AtomicReference<T> value = new AtomicReference<>(); return () -> { T val = value.get(); if (val == null) { val = value.updateAndGet(cur -> cur == null ? Objects.requireNonNull(delegate.get()) : cur); … Read more

How to remove control characters from java string?

You can do something like this if you want to delete all characters in other or control uni-code category System.out.println( “a\u0000b\u0007c\u008fd”.replaceAll(“\\p{Cc}”, “”) ); // abcd Note : This actually removes (among others) ‘\u008f’ Unicode character from the string, not the escaped form “%8F” string. Courtesy : polygenelubricants ( Replace Unicode Control Characters )

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