optional

Object cannot be converted to string when using Optional class in Java

陌路散爱 提交于 2021-02-05 07:45:22
问题 I have the following code trying to use the Optional class: import java.util.Optional; // one class needs to have a main() method public class HelloWorld { public String orelesMethod() { return "hello"; } public void test() { String value; value = Optional.ofNullable(null).orElse(orelesMethod()); System.out.println(value); } // arguments are passed using the text field below this editor public static void main(String[] args) { HelloWorld hello = new HelloWorld(); hello.test(); } } when

How can I safely upcast an Optional? [duplicate]

情到浓时终转凉″ 提交于 2021-02-04 21:38:02
问题 This question already has answers here : How to upcast object contained in Java 8 Optional? (4 answers) Closed 10 months ago . Suppose I have an Optional<Exception> and want to cast it to an Optional<Throwable> . Are there any prettier ways of doing this rather than: Optional<Exception> optionalException = Optional.of(new Exception()); Optional<Throwable> optionalThrowable = (Optional<Throwable>) (Optional<? extends Throwable>) optionalException; This solution generates a lint warning in

How can I safely upcast an Optional? [duplicate]

荒凉一梦 提交于 2021-02-04 21:37:23
问题 This question already has answers here : How to upcast object contained in Java 8 Optional? (4 answers) Closed 10 months ago . Suppose I have an Optional<Exception> and want to cast it to an Optional<Throwable> . Are there any prettier ways of doing this rather than: Optional<Exception> optionalException = Optional.of(new Exception()); Optional<Throwable> optionalThrowable = (Optional<Throwable>) (Optional<? extends Throwable>) optionalException; This solution generates a lint warning in

Java records with nullable components

大兔子大兔子 提交于 2021-02-04 15:39:07
问题 I really like the addition of records in Java 14, at least as a preview feature, as it helps to reduce my need to use lombok for simple, immutable "data holders". But I'm having an issue with the implementation of nullable components. I'm trying to avoid returning null in my codebase to indicate that a value might not be present. Therefore I currently often use something like the following pattern with lombok. @Value public class MyClass { String id; @Nullable String value; Optional<String>

JAVA8 十大新特性详解

空扰寡人 提交于 2021-02-03 00:12:22
转自https://blog.csdn.net/jiaotuwoaini/article/details/51554643#t0 --接口中的方法都是抽象的吗? --NO 场景:今天在翻看jdk的java.util.Comparator接口的时候发现了下面这段代码 1 interface Formula { 2 double calculate( int a); 3 default double sqrt( int a) { 4 return Math.sqrt(a); 5 } 6 } 我擦,什么情况? 现在带你领略下Java8的新特性: 一、接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下: 1 interface Formula { 2 double calculate( int a); 3 default double sqrt( int a) { 4 return Math.sqrt(a); 5 } 6 } Formula接口在拥有calculate方法之外同时还定义了sqrt方法,实现了Formula接口的子类只需要实现一个calculate方法,默认方法sqrt将在子类上可以直接使用。 1 Formula formula = new Formula() { 2 @Override

酷!一个仿漫画手绘风格的 Python 图表库

天涯浪子 提交于 2021-02-02 11:53:00
【导读】:关于数据可视化工具,如果你有点厌烦了常见风格(比如: Matplotlib 和 pyecharts ),那可以试试换一种风格。 本文前哨君给大家介绍一个风格完全不一样的开源库: cutecharts ,其 UI 效仿 XKCD 漫画风格,在部分场景(比如:个人作品展示)的效果或许会更好。 补充:XKCD 是一个 IT 漫画网站,在国外非常有名气。 为什么会有 cutecharts? 据 cutecharts 的开发者介绍,他个人非常喜欢一个 JS 图表库 chart.xkcd ,但它支持的图表类型不多,比 pyecharts 少很多。 Javascript 在数据交互和视觉效果上更有优势,而 Python 是一种深受数据科学界的喜爱的语言。因此,他想结合这两种技术的力量,于是就开发了 cutecharts.py。 此外,cutecharts 更多的是一个库,用来学习如何将 Javascript 世界与 Python/notebook 相结合。cutecharts 的项目结构与 pyecharts 相同,它支持 pyechart s的所有核心功能,同时更轻量级,总体上也更简洁。 GitHub 地址: https://github.com/chenjiandongx/cutecharts 简单使用 一行命令先安装好该库: pip install cutecharts 下面就是

ffmpeg 学习笔记

扶醉桌前 提交于 2021-01-30 14:52:05
ffmpeg -threads 8 -i 997.mp4 -y -c:v libx264 -c:a copy -f hls -g 60 -hls_time 2 hls_list_size 0 -hls_segment_filename test/out-%08d.ts test/out.m3u8 ffmpeg -re -i 997.mp4 -c copy -f hls -bsf:v libx264 test/out-%08d.ts test/out.m3u8 "ffmpeg","-threads","4","-i","/tmp/video/workdir/rBL7YF9hn_CEOIUNAAAAAIn7Jxk745/marker/rBL7YF9hn_CEOIUNAAAAAIn7Jxk745.mp4","-c:v","libx264","-c:a","copy","-f","hls","-g","60","-hls_time","2","-hls_list_size","0","-hls_key_info_file","/tmp/video/workdir/rBL7YF9hn_CEOIUNAAAAAIn7Jxk745/marker/keys/encrypt.keyinfo","-hls_segment_filename","/tmp/video/workdir/rBL7YF9hn

SWIFT struggling with value assignment to optional class variable [duplicate]

拟墨画扇 提交于 2021-01-29 18:21:17
问题 This question already has answers here : how to unwrap swift optionals within a struct (1 answer) What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean? (11 answers) Closed 8 months ago . as an old C programmer I am trying to get into Swift. I have already overcome many hurdles but for the following I am out of ideas. I am building a little home schooling app and want to store/retrieve the high scores to/from a JSON file. import Foundation struct HighScores:

how to deal with swiftui @State optional unwrap

依然范特西╮ 提交于 2021-01-29 07:34:52
问题 in swiftui, I have a state variable count ,which is optional, in the sheet present ,I unwrap the optional and show Detailview, but it seems never hit there. any idea how why not hit there? it seems never hit DetailView(count: num) import SwiftUI struct ContentView: View { @State var showDetailView = false @State var count : Int? var testArr = [1,2,3,4,5] var body: some View { NavigationView { List(testArr.indices){ indice in Text("row num \(indice)") .onTapGesture{ self.showDetailView = true

Returning from Java Optional ifPresent()

烂漫一生 提交于 2021-01-27 14:15:27
问题 I understand you can't return from a ifPresent() so this example does not work: public boolean checkSomethingIfPresent() { mightReturnAString().ifPresent((item) -> { if (item.equals("something")) { // Do some other stuff like use "something" in API calls return true; // Does not compile } }); return false; } Where mightReturnAString() could return a valid string or an empty optional. What I have done that works is: public boolean checkSomethingIsPresent() { Optional<String> result =