Answer by Tagir Valeev for How to get date six month before current date in...
java.time.LocalDateUsing Java 8 you can do this in single line:System.out.println(LocalDate.now().minusMonths(6).format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));If you cannot use Java 8 consider...
View ArticleAnswer by Tagir Valeev for How to access and change a java local variable...
The method parameters are very similar to local variables from the JVM point of view, so you can use the same SetLocalFloat method. You just need to specify the proper slot value. Typically, parameters...
View ArticleComment by Tagir Valeev on Why use Optional.of over Optional.ofNullable?
@tkruse the answer is correct and no amend is necessary. You may consider reading my first comment above for clarification.
View ArticleAnswer by Tagir Valeev for Code coverage finally block
In the Java bytecode (at least since Java 1.6) there's no special construct for the finally block, so it is actually duplicated many times. For example, consider the following method:public static void...
View ArticleAnswer by Tagir Valeev for How to write code in Java 11, but target Java 8...
There's a tool called Jabel created by @bsideup, which allows you to do this. It pretends to be an annotation processor so it could be plugged into javac. However, it doesn't do any actual annotation...
View ArticleAnswer by Tagir Valeev for How to zip two Java Lists
Here's Java-8 solution using the Pair class (like in @ZhekaKozlov answer):public static <A, B> List<Pair<A, B>> zipJava8(List<A> as, List<B> bs) { return...
View ArticleAnswer by Tagir Valeev for How to escape the (hash) # sign in a GitHub...
Add an extra space after ### C\#.
View ArticleAnswer by Tagir Valeev for Trying to read from the console in Java
Normally, the Console object requires a valid tty provided by an operating system, but IntelliJ IDEA output window does not provide one. It's not very trivial to do this in OS-independent way. Not only...
View ArticleAnswer by Tagir Valeev for Parallelism with Streams created from Iterators
There was a flaw in Spliterators.spliteratorUnknownSize() implementation. I fixed it in Java 19, see JDK-8280915. Since 19-ea+19-1283 early access build the problem is not reproduced anymore,...
View ArticleAnswer by Tagir Valeev for Why stream created with Spliterators is not being...
There was a flaw in Spliterators.spliteratorUnknownSize() implementation. I fixed it in Java 19, see JDK-8280915. Since 19-ea+19-1283 early access build the problem is not reproduced anymore, your code...
View ArticleAnswer by Tagir Valeev for IntelliJ Refactor to pull variable definition...
Since IntelliJ IDEA 2021.3 (EAP builds are already available) it's possible just to complete the variable outside of try block to bring it into the scope:try { IndexResponse indexResponse =...
View ArticleStrange "!*" entry in LocalVariableTypeTable when compiling with Eclipse...
Let's compile the following code with ECJ compiler from Eclipse Mars.2 bundle:import java.util.stream.*;public class Test { String test(Stream<?> s) { return s.collect(Collector.of(() -> "",...
View ArticleAnswer by Tagir Valeev for Does the JDK provide a dummy consumer?
No, JDK does not provide dummy consumer as well as other predefined functions like dummy runnable, always-true predicate or supplier which always returns zero. Just write t -> {}, it's anyways...
View ArticleAnswer by Tagir Valeev for Java Streams - Get a "symmetric difference list"...
Something like this may work:Stream.concat(bigCarList.stream(), smallCarList.stream()) .collect(groupingBy(Function.identity(), counting())) .entrySet().stream() .filter(e ->...
View ArticleAnswer by Tagir Valeev for How do I read / convert an InputStream into a...
For completeness here is Java 9 solution:public static String toString(InputStream input) throws IOException { return new String(input.readAllBytes(), StandardCharsets.UTF_8);}This uses the...
View ArticleAnswer by Tagir Valeev for Is there a common Java utility to break a list...
In case you want to produce a Java-8 stream of batches, you can try the following code:public static <T> Stream<List<T>> batches(List<T> source, int length) { if (length <=...
View ArticleAnswer by Tagir Valeev for How to force max to return ALL maximum values in a...
I implemented more generic collector solution with custom downstream collector. Probably some readers might find it useful:public static <T, A, D> Collector<T, ?, D> maxAll(Comparator<?...
View ArticleAnswer by Tagir Valeev for CERK07- SPOJ - bitmask in bfs
The result of (umask & 0x2) expression is either 0 or 2. It cannot be 1, hence IntelliJ IDEA warns you about this. Similarly (umask & 0x4) is either 0 or 4 and (umask & 0x8) is either 0 or...
View ArticleAnswer by Tagir Valeev for Can IntelliJ convert my old string concatenation...
Disclosure: IntelliJ IDEA developer is here.Currently, IntelliJ IDEA only suggests converting the complete literal concatenation into the text block. Here, however, you have a concatenation that...
View ArticleAnswer by Tagir Valeev for Improve Intellij Code Inspection for may produce...
Disclosure I'm IntelliJ IDEA developer responsible for this subsystemNo, it's not possible now. There's no better solution than possible workarounds you already listed, assuming that you cannot change...
View Article