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 3 years ago.
Improve this question
I am trying to rewrite my if else statement so I skip the //do nothing part but I can't get my around to find the opposite of the if statement.
someone please help?!
if (decision.equals("repay")){
String riskClass = null;
if (doc.hasItem("riskclass")){
riskClass = doc.getItemValueString("riskclass");
}
if ( (null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay")) ){
//do nothing
} else{
//do something
}
}
You can simply invert the condition. Try the snippet below.
if ( !((null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay"))) ){
//do something
}
Another way to invert the check is invert individual conditions and replace or's with and's:
if ( (null != riskClass) && (!riskClass.equals("")) && (!riskClass.equals("repay")) ){
Another point:
Avoid call equals on variables passing constants: the constant will never be null. So in this particular case would be better write:
if (!"repay".equals(riskClass) && !"".equals(riskClass))
Related
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 10 months ago.
Improve this question
Why doesn't this solution work for finding valid anagram?
26/36 test cases get passed in LeetCode.
class Solution {
public boolean isAnagram(String s, String t) {
int sASCII = 0, tASCII = 0;
if(s.length() != t.length()) {return false;}
else{
for(int i = 0 ; i < s.length(); i++){
sASCII += (int)s.charAt(i);
tASCII += (int)t.charAt(i);
}
}
if(sASCII == tASCII){
return true;
}
return false;
}
}
The sums tASCII and sASCII can be equal even if the numbers are not anagrams. Let's say that you can get the number 100 by adding 60+40, but you can also get it by adding 70+30, so i recommend to use a HashMap to note every occurence of every letter or to sort the strings as arrays of chars and then compare them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I've multiple List of Java-Datetimes with MyCustomDate object contains ( start DateTime and end DateTime ) LocalDateTime with hours and seconds.
I need to write function to check if There is Overlapping dates in my list return true \ false - with the best performance
can use java8 (stream functions)
You have to test all ranges aginst each other. There is no other way. Try this way, although is just one implementation, there are plenty of correct ways to implement it:
public boolean isThereOverlapingRanges(List<CustomDateRange> ranges) {
if (ranges.size() <= 1) {
return false;
}
boolean overlaping = false;
for(int i = 1; i < ranges.size(); i++) {
if (isDateInRange(ranges.get(0).start(), ranges.get(i)) ||
isDateInRange(ranges.get(0).end(), ranges.get(i))) {
overlaping = true;
break;
}
}
return overlaping || isThereOverlapingRanges(ranges.subList(1, ranges.size());
}
private boolean isDateInRange(DateTime date, CustomDateRange range) {
return date.after(range.start()) && date.before(range.end());
}
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
So I have just gotten a bunch of old code where I should improve the performance of. After looking for a while I came accross this piece of code:
//Reference on how the global Variables look like:
private static final int PLS_4_SYSTEM_MALFUNCTION_FL = 4;
private static final int SPS1_20_PUFFER_EMPTY = 20;
private static final int SPS1_30_STOPPERFL_LOCKED = 30;
//...
int disturbance1 = bMessage.getDisturbanceSps1().intValue();
int disturbance2 = bMessage.getDisturbanceSps1().intValue();
if (disturbance1 == SPS1_20_PUFFER_EMPTY && disturbance2 == 0) {
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
}
if (disturbance1 == SPS1_30_STOPPERFL_LOCKED && disturbance2 == 0) {
checkWarnings(bMessage, PLS_6_STOPPERFL_LOCKED);
}
if (disturbance1 == SPS1_40_DISTURBANCEFL && disturbance2 == 0) {
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
}
//...
This is just a small example of the code. There are like 300+ more lines that goes on like this.
Now I know that there are many answers on the web already, and I have looked at some of them, but I am unsure which answer would help me the best in this case.
You could change that to something like enums, new-style switches etc. This can certainly make the code more readable and maintainable.
However, you were talking about enhancing performance. Changing this code most likely will have hardly any impact on performance, at least not in a positive way.
If you want to improve performance, measure and find the bottlenecks in the code and focus on those.
this might help !!
if(disturbance2 == 0) {
switch (disturbance1){
case SPS1_20_PUFFER_EMPTY:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_30_STOPPERFL_LOCKED:
checkWarnings(bMessage, PLS_5_PUFFER_EMPTY);
break;
case SPS1_40_DISTURBANCEFL:
if (bMessage.getDisturbanceType().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_SHORT) {
checkWarnings(bMessage, PLS_3_SHORTDISTURBANCE_FL);
}
else if (bMeldung.getStoerungsartMde().intValue() == DISTURBANCETYPE_SYSTEMDISTURBANCE_LONG) {
checkWarnings(bMessage, PLS_4_SYSTEMMALFUNCTION_FL);
}
break;
}
}
also you can write
if(disturbance2 != 0){
// return or exit if requires
}
// then start switch() , you can remove one branch of block
Switch on disturbance1 can help.
Check out the new switch style from Java17. It makes code really lean.
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 7 years ago.
Improve this question
if ((A!= null || A!= "") && (B!= null || B!= "")
&& (C!= null || C!= "")elseif...elseif...elseif...
How we can do this without if else condition or in minimum code ??
Say you have a List<String> and any number could be null or empty you can do
List<String> values = ...
List<String> good = values.stream()
.filter(s -> s != null && !s.isEmpty())
.collect(Collectors.toList());
Instead of having lots of variables, you are better off having an appropriate collection.
I would group all the strings in a stream and then apply the same logic to each element:
Stream<String> strings = Stream.of(a, b, c, ...);
if (strings.allMatch(s -> s != null || s != "")) {
//
}
Note: I've changed variable names to a, b, c, etc., since Java conventions establish that variable names should be lowerCamelCase.
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 trying to use this boolean method in my app. I want to use what it returns in an if statement so if it returns true I can do something. Something like
if(isNook() == True) {
//do something
}
I'm sure the answer is obvious but I couldn't find anything on how to do this.
Why don't you use:
if (isNook()) {
// do something
}
People are suggesting this:
if (isNook()) {
// do something
}
which is correct. However, we need to discuss your attempt. This was it:
if(isNook() == True) {
// do something
}
In Java, boolean variables are represented with true and false - NOT True and False - the values are case sensitive.
To contrast, for example, Python uses the values True and False
Your attempt should have been this:
if(isNook() == true) {
// do something
}
Try this:
boolean bool_result;
if(bool_result = isNook( )) {
// do stuff with bool_result here
}
result can actually be other data types (i.e. int/char/string) as long as it evaluates to true or false.