What’s the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

Consider these filenames: C:\temp\file.txt – This is a path, an absolute path, and a canonical path. .\file.txt – This is a path. It’s neither an absolute path nor a canonical path. C:\temp\myapp\bin\..\\..\file.txt – This is a path and an absolute path. It’s not a canonical path. A canonical path is always an absolute path. Converting … Read more

Should I always use a parallel stream when possible?

A parallel stream has a much higher overhead compared to a sequential one. Coordinating the threads takes a significant amount of time. I would use sequential streams by default and only consider parallel ones if I have a massive amount of items to process (or the processing of each item takes time and is parallelizable) … Read more

How to convert comma-separated String to List?

Convert comma separated String to List List<String> items = Arrays.asList(str.split(“\\s*,\\s*”)); The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas. Please note that this returns simply … Read more

Android changing Floating Action Button color

As described in the documentation, by default it takes the color set in styles.xml attribute colorAccent. The background color of this view defaults to the your theme’s colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList). If you wish to change the color in XML with attribute app:backgroundTint … Read more

Is it possible to use Java 8 for Android development?

UPDATE 2017/11/04 – Android Studio 3.0 now has native support for Java 8. gradle-retrolambda is now no longer needed. See https://developer.android.com/studio/write/java8-support.html The above link also includes migration instructions if you are using gradle-retrolambda. Original answer below: Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still … Read more

Create instance of generic type in Java?

You are correct. You can’t do new E(). But you can change it to private static class SomeContainer<E> { E createContents(Class<E> clazz) { return clazz.newInstance(); } } It’s a pain. But it works. Wrapping it in the factory pattern makes it a little more tolerable.

How to build jars from IntelliJ properly?

Here’s how to build a jar with IntelliJ 10 http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/ File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies… Select a Main Class (the one with main() method) if you need to make the jar runnable. The above sets the “skeleton” to where … Read more

Find first element by predicate

No, filter does not scan the whole stream. It’s an intermediate operation, which returns a lazy stream (actually all intermediate operations return a lazy stream). To convince you, you can simply do the following test: List<Integer> list = Arrays.asList(1, 10, 3, 7, 5); int a = list.stream() .peek(num -> System.out.println(“will filter ” + num)) .filter(x … Read more