Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by Tagir Valeev
In Memory of Stiver Original
On October 20, the original author of the Fernflower Java decompiler, Stiver, passed away after a long fight against glioblastoma. Stiver was a German programmer of Russian origin, primarily developing software for medical equipment. Twenty years ago, he developed a deep professional interest in Java virtual machine internals. You may remember his research blog posts, like this one (in Russian) from 2006 about how to tweak the Java class hierarchy using Unsafe when Java 1.5 was just released.
Java 8 Stream: difference between limit() and skip()
What you have here are two stream pipelines. The stream pipelines consist of a source, several intermediate operations, and a terminal operation. But the intermediate operations are lazy. This means that nothing happens unless an upstream operation requires an item. When it does, then the intermediate operation does all it needs to produce the required item, and then again waits until another item is requested, and so on. The terminal operations are usually "eager".
Java 8: Reference to [method] is ambiguous
Your problem is a side-effect of Generalized Target-type Inference, an improvement in Java 8. Let's take your example method, Now, in the method above, the generic parameter cannot be resolved by the compiler because there's no parameter with . So, they introduced a concept called , which allows the parameter to be inferred based on the assignment parameter. So, if you do, This works well in Java 7. But the following does not, Because type inference worked only for direct assignments.
Java Generics enforcing compatible wildcards
I've these classes. class RedSocket {} class GreenSocket {} class RedWire {} class GreenWire {} I've a class which uses 2 generic types public class Connection<W, S> {} where W is Wire type & S is Socket type. I'm trying to enforce compile time check to ensure that socket & wire have the same color.
Generic bounded wildcard of function input type
Consider you want to map a CharSequence to another CharSequence (thus T = R = CharSequence). Which functions will work for you? Function<Object, String> fn1 = Object::toString; Is it good for you? Yes, because it can take any CharSequence (which is also Object) and convert it to the String (which is also CharSequence). Function<CharSequence, StringBuilder> fn2 = StringBuilder::new; Is it good for you?
Java 8 stream - Generic bounded wildcard of function input type
Consider you want to map a CharSequence to another CharSequence (thus T = R = CharSequence). Which functions will work for you? Function<Object, String> fn1 = Object::toString; Is it good for you? Yes, because it can take any CharSequence (which is also Object) and convert it to the String (which is also CharSequence). Function<CharSequence, StringBuilder> fn2 = StringBuilder::new; Is it good for you?
Generic Type cast
For the completeness of this resource, here's the difference in compiled bytecode between a cast to a generic: And an explicit cast to a with no generics: You can see that the version with generics doesn't perform the instruction at all (thanks to type erasure, so you shouldn't expect an exception when giving it data with an unmatched class.
Which would be better in terms of performance Lambda or simple loop?
It allows you write in one line (while having enough readability) something, what was not possible before. Performance is not issue here stays . For example in my Windows Phone App, I have sessions and in that sessions are performers and I want to select all sessions which have one concrete performer (like you want to see all the movies some actor plays in). In Java 1.7 or less I had to create loop with inner loop, checking for it, returning null if there is no performer etc.
Morbid use of constants
You should not write ! Neither should you write (see exception below)! Why? A literal like is called a magic number. Magic numbers are "unique values with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants" (explanation from the same Wikipedia page). So what usually should be done is making those magic numbers to constants whose name represents or explains the meaning of that number. The constant does not explain the meaning.
instanceof operator behaves differently for interfaces & classes
Because Java has no multiple class inheritance it's absolutely known during the compilation that object of type cannot be subtype of . On the other hand it possibly can be subtype of interface , for example in this case: So looking only at expression compiler cannot tell in advance whether it will be true or false, but looking at it knows that this is always false, thus meaningless and helps you to prevent an error.
The 'instanceof' operator behaves differently for interfaces and classes
Because Java has no multiple class inheritance it's absolutely known during the compilation that object of type cannot be subtype of . On the other hand it possibly can be subtype of interface , for example in this case: So looking only at expression compiler cannot tell in advance whether it will be true or false, but looking at it knows that this is always false, thus meaningless and helps you to prevent an error.
A better approach to handling exceptions in a functional way
Presented below is the full code of the class. It has a quite large API which is a pure extension of the API so it can be a drop-in replacement for it in any existing code—except that it isn't a subtype of the final class. The class can be seen as being in the same relationship with the monad as is with the monad: it draws inspiration from it, but is adapted to the Java idiom (such as being stateful and actually throwing exceptions, even from non-terminal operations).
If char array is an object in java, why printing it does not return its hash code?
First of all, a char array is an Object in Java just like any other type of array. It is just printed differently. (which is the type of the instance) has a special version of for character arrays - - so it doesn't have to call for that array. It eventually calls , which writes the characters of the array to the output stream. That's why calling println for a behaves differently than calling it for other types of arrays.
java:Split String into smaller String and again into key:Value pairs
Your program is absolutely fine. Just because you asked for a more optimal code. I reduced your memory by taking few variables instead of taking arrays and storing in them. Look at your string it follows a patter. key : value :: key : value ::.... What can we do from this? get the key till it is : , once it reaches : get value until it reaches '::'.
What's wrong with the unchecked cast?
The list.toArray idiom you employ here is not parametrized by an array of your List's parametrized type, so it returns Object[]. For instance, with your List<String> str, you can invoke: String[] foo = str.toArray(new String[str.size()]); without casting. The problem here is that, due to Java generics' design, you can never initialize a new E[], hence you have to cast to (E[]). I cannot see this throwing a ClassCastException ever as is.
Java 8 stream api: Exceptions when modifying Lists
Adding some debug prints to the pipeline shows the source of the NullPointerException : list.stream().peek(string -> System.out.println("peek1 " + string)).filter(string -> string.equalsIgnoreCase("5")).peek(string -> System.out.println("peek2 " + string)).forEach(string -> removeMember(string)); This outputs : peek1 0 peek1 1 peek1 2 peek1 3 peek1 4 peek1 5 peek2 5 peek1 7 peek1 8 peek1 9 peek1 null Exception in thread "main" java.lang.NullPointerException at...
Checking equality of three or more Strings in Java
There are different ways to approach this problem depending on the number of objects being compared.
Is StringBuilder threadsafe (using it with parallelStream)?
The better solution is to use events.parallelStream().map(event -> event+"\n").collect(Collectors.joining()); Or alternatively (thanks to @Holger): events.parallelStream().map(Object::toString).collect(Collectors.joining("\n", "", "\n")); In general avoid using forEach as terminal operation for streams. Usually reduction operations like collect or reduce are better alternatives.
SimpleDateFormat.parse() - generates wrong date for different date-formats
If you can afford using Java 8 time API, its formatter works as expected: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); try { LocalDate date = LocalDate.parse("2030-05-21", formatter); System.out.println(date); LocalDate date2 = LocalDate.parse("05-21-2030", formatter); System.out.println(date2); } catch (DateTimeParseException e) { e.printStackTrace(); } Output: 2030-05-21 java.time.format.DateTimeParseException: Text '05-21-2030' could not be parsed at index 0 at...
Why can't @FunctionalInterfaces have default methods?
A functional interface is an having a single abstract method. The entire purpose of defining functional interfaces is to enable the implementation of the single abstract method via lambda expressions which will effectively override that method which makes providing a implementation for it pointless. Having an consisting entirely of methods raises multiple problems.
Referencing non-final variable: why does this code compile?
First off, I apologise if this is a duplicate question. I found many similar ones, but none that directly address my question. In preparation for an upcoming exam, I am doing a past paper. It has a question that gives a code snippet. We have to state if it compiles, and if not, write the line at which the first compiler error occurs and explain it. This is the snippet: I was expecting it not to compile as is not .
How to use Java 8 streams to find all values preceding a larger value?
It's certainly nicer than an imperative solution with a large loop, but still a bit meh as far as the goal of "using a stream" in an idiomatic way. Is it possible to retrieve the next value in a stream? Nope, not really. The best cite I know of for that is in the package description: The elements of a stream are only visited once during the life of a stream. Like an , a new stream must be generated to revisit the same elements of the source. We could also technically do it in a couple other ways:
Java logical operator (&&, ||) shortcut mechanism
As I was reading a colleague's Java code, I stumbled upon an army of if/else statements. In those statement, several && and || operators were fighting each other without any help from parenthesis. "So what ?", no problem here. Well... I simplified the those statements for you: According to you, what would the result be ? Honestly, I thought it would be false, but it seems the mysterious ways of short-circuiting don't work like that. In this case, the result if true.
Java logical operator (&&, ||) short-circut mechanism
As I was reading a colleague's Java code, I stumbled upon an army of if/else statements. In these statements, several and operators were fighting each other without any help from parenthesis. I simplified the statements into: What do you think the result would be? Honestly, I thought it would be , but it seems short-circuiting doesn't work like I expected. In this case, the result is . The short-circuit mechanism seems to consider the whole expression as when it finds immediately followed by .
Why does lambda translation need generation of a static method?
Your test is incomplete.
Why calling method with generic return on a generic class is considered unsafe by javac?
Consider the following code: public class Main { public static class NormalClass { public Class<Integer> method() { return Integer.class; } } public static class GenericClass<T> { public Class<Integer> method() { return Integer.class; } } public static void main(String...
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with Tagir
Contact Tagir, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack