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 doing some validation using simple Java, I am coding as below
if (something.equals("ABC"))
expectedvalue = "TEST1";
else if(something.equals("DEF"))
expectedvalue = "TEST5";
// and so on....
My issue here is that, if something.equals("ABC"), expectedvalue can be any of these two values -> "TEST1" or "TEST2"
During validation, in the output, I do not get error if I give "TEST1" for expectedvalue. It should do the same even if I give "TEST2". How to do that?
You have to have a Set of allowed values.
Set<String> allowed = new HashSet<String>();
allowed.put("TEST1");
allowed.put("TEST2");
//and then you can use this set
if (allowed.contains(yourValueToCheck)) {
// do what you need
Also note that it is safer to compare strings like this:
if ("ABC".equals(something)) // to protect yourself from NullPointerExceptions
Perhaps you could change your test to test with an OR statment?
if(expectedvalue.equals("TEST1") || expectedvalue.equals("TEST2"))
.....
Related
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 3 years ago.
Improve this question
List<String> listOfNames = new ArrayList<>();for(
String name:listOfNames) { ///WANT TO PASS THIS VALUE IN THIS FUNCTION AND THIS FUNCTION WILL BE RUN IN PARALLEL FOR ALL INDEXES
myfunct(name){
//dosomething
}
A simple
listOfNames.parallelStream().forEach( name -> {
// do something
});
would already do.
The point here is: there are zillions of ways doing that, and they are all extensively documented. I suggest you start here for example. And as Andy correctly stated: "just adding threads" isn't necessarily an improvement. More threads do not automatically mean "better". To the contrary, they make code harder to follow and understand, and open a whole universe of adding potential bugs.
List<String> listOfNames = asList("a", "b", "c");
listOfNames
.parallelStream()
.forEach(System.out::print);
will result in e.g.
bca
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 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
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 6 years ago.
Improve this question
I want to set a property to an other property that could be null. I wonder what is faster, to wrap it around an if statement or simply just set the property without the check.
with if statement
if (foo.getName() != null) {
bar.setFoo(foo.getName());
}
simple
bar.setFoo(foo.getName()); //getName() can be null
If you are going to setFoo() either way, you might as well just set it and skip the condition. I would use the condition if you wanted to take an action if !null and a different action if null.
when foo.getName() != null would return true, the setter would be called, so you will have 2 commands that will be executed, however the second example will execute only 1 command. I think the second one is more performant
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
Is possible to do in Java something like this? :
System.out.... -> ask for a string
Store this string
Create a variable with the name of this string
Thank you very much!
No. You cannot create a variable, at execution time, with a particular name.
However, you can use a Map, which probably does what you want. The get method will look up an entry, and the put method will set an entry. Example:
Map<String, Integer> myMapOfThings = new HashMap<>();
String nameOfThing = /* ... get a string from the user somehow here ... */;
myMapOfThings.put(nameOfThing, 2);
System.out.println(myMapOfThings.get(nameOfThing)); // prints 2
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 have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.