Why java has no not function/ alternatives for same [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Ok so if you want to check if a list is not empty
we would need to do something like
if(! mylist.isEmpty())
this affects code readability, so how can we write the same thing in a readable way, calling out negation of condition check.
One of possibility is to have a static helper function like:
static boolean not(boolean condition) { return !condition;}
How bad is this idea? Are there other options in apache common or guava etc? Or any other way you have achieved this?

It is not a bad idea in itself and it will not affect anything.
However I think many people will disagree with you regarding the "unreadability" of the normal way of negating things using !.
if ( ! list.isEmpty() )
versus
if ( not( list.isEmpty() ) )
does not make much of a difference IMHO.

Related

Is converting "Integer to String" by appending it to double quotes like 'return ("" + 2)' costly operation? If yes, what is alternative? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have integer status response from a REST API. I need to return string, so I use it like
return "" + statusCode
Is it fine way to do that or is it costly? Or should I use String.valueOf() or is there another alternative?
No need - if you decompile the class file, you'll see that the compiler does this for you anyway. I think this makes the code very readable, however you might want to ask yourself why are you doing this anyway... if you are confident that returning a number as a string from a method is correct, this is a good way to do it, IMO.

How to achieve the same result as with filter and findFirst [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Is there a more elegant way to achieve by using java 8 or above the eighth version what's below?
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter(s -> s.equals("test")).findFirst();
Thanks in advance
It depends on what exactly you want to do.
If you just want to check if "test" is in one of the elements, you could just use .contains():
List.of("test","test1").contains("test");
If you want to find the first element fitting a condition, you can omit creating the list and directly create a Stream:
Stream.of("test","test1").filter(s->"test".equals(s)).findFirst()
If you want to check if an element fitting the condition exist, you can use anyMatch:
Stream.of("test","test1").anyMatch(s->"test".equals(s))
This is probably the best your going to get.
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter("test"::equals).findFirst();
If its reused you can just make a method out of the 2nd line. Question has also already been answered here

What is the "proper" form in which to write Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Personally, I write my Java like this:
class xyz {
...
}
OR
if (condition) {
...
}
etcetera...
In so many examples, I see code written like this:
class xyz
{
...
}
OR
if (condition)
{
...
}
etcetera...
To me, my way makes the most sense because not only does it take less lines to write, but (again, in my opinion) looks more proper and professional. I'd love to know people's opinions - and reasoning behind them - as to which form is better and why.
The first one with opening braces on the same line is almost the standard Java programming guideline for years.
The original Sun Java guideline, that was never updated since 1999.
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html
Google Java guideline
https://google.github.io/styleguide/javaguide.html#s4.1-braces
Android style
https://source.android.com/source/code-style.html
On the opposite side, many open source platforms like Apache/maven advocate brace in new line

Rearranging GOTO's using ASM [JAVA] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am looking at an obfuscated application, and the application seems to have a lot of GOTO's which I want to rearrange or remove
The question is, how would I go about doing this?
for(final MethodNode mn : classNode.methods) {
final BIF is = new BIF(mn); //BIF is my bytecode instruction finder
AbstractInsnNode ain;
while ((ain = is.next()) != null) {
if (ain instanceof JumpInsnNode && ain.getOpcode() == GOTO) {
final JumpInsnNode jump = (JumpInsnNode) ain;
mn.instructions.remove(jump);
removed++;
}
}
}
So I've tried just removing them all, but it doesn't seem to work and I don't know how to rearrange them
I don't think you'll be able to solve this easily, it looks like a quite radical obfuscation mechanism. You could try to un-goto it, linearizing the code by defragmenting the chunks between goto jumps. But then, some goto's are legitimate flow control jumps, so you'll need a way to detect this. It looks like a real challenge, one that will take much of your time. But, maybe the challenge is intruguing enough to push forward :)

Why String.contains() can not accept a regex as a parameter? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Any idea to Implementing this?
Is this best way to do?
boolean contains=string.split(regex,2).length==2;
Thanks a lot for any suggestion.
You'd usually use something like:
boolean contains = pattern.matcher(text).find();
where pattern is an instance of java.util.regex.Pattern.
This is easily implemented in terms of Matcher.find():
public static boolean containsRegex(String input, String regex) {
return Pattern.compile(regex).matcher(input).find();
}
However, it is rarely necessary as you can simply stay with matches, slightly expanding your regex to begin and end with .*.
I would use
boolean contains = Pattern.compile(regex).matcher(string).find();

Categories