This question already has answers here:
Should Optional.ofNullable() be used for null check?
(3 answers)
Null check using Optional
(1 answer)
Null check vs Optional isPresent check
(3 answers)
Uses for Optional
(14 answers)
Closed 2 years ago.
I've a nullable field where I want to perform operation, if not null. return null otherwise.
I can write it in plain Java as
return (myfield != null)? myfield.getData() :null;
How can I write the same code in one line with Optional
Something like below.
Below code is not a correct code, just want to ask the correct version of it
return Optional.ofNullable(myfield).map(Functionas.identity().getData()).orElse(null);
Related
This question already has answers here:
How do I use the conditional operator (? :) in Ruby?
(7 answers)
Closed 1 year ago.
this might be a very vague question. But i am wondering if someone could translate this into pseudocode:
a = (1 + (bool ? rand(13) : 0)
Does it mean that a will become any value between 0-13 + 1? what is the purpose of the boolean value and the question mark?
(true ? rand(13) : 0) mean (if true then rand(13) else 0 end)
if you have directly "true" in condition, the "else" is never called (is useless), you can write : a = 1 + rand(13) directly ;)
rand(13) give random int between 0 and 12 ;)
if you want "13" put rand(14)
personally I always use range like this (all range is include, it's easier to understand) : rand(0..13)
This question already has answers here:
Defining a method for a struct only when a field is a certain enum variant?
(1 answer)
Is there a way to use existing structs as enum variants?
(2 answers)
Can struct-like enums be used as types?
(2 answers)
Closed 2 years ago.
With this definition of Strct, it is possible for values to hold any variant of the Test enum:
enum Test {
A,
B,
C,
}
struct Strct {
values: Vec<Test>,
}
Is there a way to enforce it to hold only a specific variant of the enum? E.g. Vec<A> or Vec<B> or Vec<C> for that specific case.
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")
This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".
This question already has answers here:
When to use .First and when to use .FirstOrDefault with LINQ?
(14 answers)
Closed 6 years ago.
i am working with LINQ there i use First and FirstOrDefault both return the same result what is the difference
First and Last throw an exception if no result was found.
FirstOrDefault and LastOrDefault return the result variable type's default value in that case.
FirstOrDefault and LastOrDefault would not throw an exception if there are no search item in sequence, they will return null.