optional

Collectors.reducing to List

可紊 提交于 2020-06-25 10:15:07
问题 Consider this class: @Data @AllArgsConstructor @NoArgsConstructor class User { String name; String languages; } I have a List<User> and I would like to reduce on languages. Input: List<User> list = new ArrayList<>(); list.add(new User("sam", "java")); list.add(new User("sam", "js")); list.add(new User("apollo", "html")); Expected output: [User(name=apollo, languages=html), User(name=sam, languages=java, js)] I can achieve this using following code: List<User> l = list.stream() .collect

If else code execution with Optional class

人走茶凉 提交于 2020-06-25 09:16:28
问题 I was going through a tutorial of Optional class here - https://www.geeksforgeeks.org/java-8-optional-class/ which has the following String[] words = new String[10]; Optional<String> checkNull = Optional.ofNullable(words[5]); if (checkNull.isPresent()) { String word = words[5].toLowerCase(); System.out.print(word); } else{ System.out.println("word is null"); } I am trying to make it of less lines using ifPresent check of Optional as Optional.ofNullable(words[5]).ifPresent(a -> System.out

What's the advantage of `std::optional` over `std::shared_ptr` and `std::unique_ptr`?

*爱你&永不变心* 提交于 2020-06-25 01:28:10
问题 The reasoning of std::optional is made by saying that it may or may not contain a value. Hence, it saves us the effort of constructing a, probably, big object, if we don't need it. For example, a factory here, will not construt the object if some condition is not met: #include <string> #include <iostream> #include <optional> std::optional<std::string> create(bool b) { if(b) return "Godzilla"; //string is constructed else return {}; //no construction of the string required } But then how is

unexpectedly found nil while unwrapping an Optional

扶醉桌前 提交于 2020-06-17 03:09:04
问题 @IBOutlet weak var groupNameTF: UITextField! var group: Group? { didSet { groupNameTF.text = group?.name } } Can't understand what the problem with optional here. As I see from logs, group isn't nil . As I thought I do safe value unwrapping. I also checked with if let construction, same result. 回答1: Most likely that happens because groupNameTF is nil. A quick workaround is to protect that with an if: var group: Group? { didSet { if groupNameTF != nil { groupNameTF.text = group?.name } } } 回答2

unexpectedly found nil while unwrapping an Optional

依然范特西╮ 提交于 2020-06-17 03:07:27
问题 @IBOutlet weak var groupNameTF: UITextField! var group: Group? { didSet { groupNameTF.text = group?.name } } Can't understand what the problem with optional here. As I see from logs, group isn't nil . As I thought I do safe value unwrapping. I also checked with if let construction, same result. 回答1: Most likely that happens because groupNameTF is nil. A quick workaround is to protect that with an if: var group: Group? { didSet { if groupNameTF != nil { groupNameTF.text = group?.name } } } 回答2

Parameter of Optional.orElse gets called even when Optional contains value [duplicate]

那年仲夏 提交于 2020-06-12 15:24:31
问题 This question already has answers here : When I need to use Optional.orElseGet() over Optional.orElse() (3 answers) Closed 2 years ago . The below code prints: should Not have called hello world Why is orElse getting executed even when we have an Optional that contains a value? public static void main(String[] args){ String o = Optional.ofNullable("world").map(str -> "hello" + str).orElse(dontCallMe()); System.out.println(o); } private static String dontCallMe() { System.out.println("should

How does Optional covariance work in Swift

荒凉一梦 提交于 2020-06-10 02:43:22
问题 How does covariance work for Optional s in Swift? Say I write the following code: var nativeOptionalView: Optional<UIView> let button = UIButton() nativeOptionalView = .Some(button) var nativeOptionalButton = Optional.Some(button) nativeOptionalView = nativeOptionalButton It compiles and works just fine. However if I define MyOptional as enum MyOptional<T> { case Some(T) case None } And write the following: var myOptionalView: MyOptional<UIView> let button = UIButton() myOptionalView = .Some

How does Optional covariance work in Swift

邮差的信 提交于 2020-06-10 02:43:20
问题 How does covariance work for Optional s in Swift? Say I write the following code: var nativeOptionalView: Optional<UIView> let button = UIButton() nativeOptionalView = .Some(button) var nativeOptionalButton = Optional.Some(button) nativeOptionalView = nativeOptionalButton It compiles and works just fine. However if I define MyOptional as enum MyOptional<T> { case Some(T) case None } And write the following: var myOptionalView: MyOptional<UIView> let button = UIButton() myOptionalView = .Some

Scala Option(null) expected as None but I got Some(0)

浪子不回头ぞ 提交于 2020-06-08 16:26:07
问题 val i: java.lang.Integer = null val o: Option[Int] = Option(i) // This yields Some(0) What is the safe way to convert null: java.lang.Integer to Scala Option[Int] ? 回答1: You are mixing Int and java.lang.Integer so val i: java.lang.Integer = null val o: Option[Int] = Option(i) implicitly converts to val o: Option[Int] = Option(Integer2int(i)) which becomes val o: Option[Int] = Option(null.asInstanceOf[Int]) thus val o: Option[Int] = Some(0) If you want to work with java.lang.Integer , then

How to create a generic function in Swift that will reject the given parameter unless it is an Optional?

旧巷老猫 提交于 2020-05-17 06:56:06
问题 This question is a follow up to my earlier question: I expected the system to report non protocol conformance, but it does not! Why? Please read the referred question for you to get a better idea of the constraints at hand. I created a generic function in Swift that will reject its parameter unless such a parameter is Optional . The function I created fully works and does what I desire. Meaning, any calls to onlyCallableByAnOptable(...) , even inside an if let , will yield error due to non