无法将流对象包装在一个try/catch block.
我试过这样的:
reponseNodes.stream().parallel().collect(Collectors.toMap(responseNode -> responseNode.getLabel(), responseNode -> processImage(responseNode)));
Run Code Online (Sandbox Code Playgroud)
Eclipse开始抱怨下划线processImage(responseNode)并建议它需要Surround with try/catch.
然后我更新到:
return reponseNodes.stream().parallel().collect(Collectors.toMap(responseNode -> responseNode.getLabel(), responseNode -> try { processImage(responseNode) } catch (Exception e) { throw new UncheckedException(e); }));
Run Code Online (Sandbox Code Playgroud)
更新的代码也不起作用.
我有一个父控制器,其中包含一个按钮.当我点击按钮时,它打开新窗口并将一些数据显示在表格中.我用于打开窗口的代码是
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("../layout/SearchCustomer.fxml"));
Parent parent = (Parent) fxmlLoader.load();
Scene scene = new Scene(parent);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(parent.getScene().getWindow());
stage.setScene(scene);
stage.resizableProperty().setValue(false);
stage.showAndWait();
Run Code Online (Sandbox Code Playgroud)
它正确打开窗口.现在我需要的是,当我双击子窗口的表行时,它应该在父控制器文本框中设置一些值.我们如何将这个值从子控制器传递给父控制器?
是否java.util Optional.ofNullable工作正常使用的Mockito?
在代码执行期间,我遇到这样的事情:
User user = Optional.ofNullable(userProviderMock.findUser()).orElse(someMethod())
Run Code Online (Sandbox Code Playgroud)
我设置我的模拟行为如下:
when(userProviderMock.findUser()).thenReturn(new User());
Run Code Online (Sandbox Code Playgroud)
当我运行它时,userProviderMock返回new User()(调试确认),但不知何故someMethod()仍然执行.我真的不知道为什么会这样.有线索吗?
在Class中Site,我有两种实用方法.
第一个,如果没有错误发生parseStub,则解析Site为a ; 否则,它返回.使用:Master nullOptional
public static Optional<Master> parseStub(Site site) {
// do some parse work; return Optional.empty() if the parse fails.
}
Run Code Online (Sandbox Code Playgroud)
第二种方法parseStubs是解析的列表Site到的列表Master.它重用parseStub,并且必须处理可能为空的Optional<Master>:
public static List<Master> parseStubs(List<Site> sites) {
return sites.stream()
.<Master>map(site -> Site.parseStub(site).orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的代码中,我
null再次介绍.
我怎么能避免null(和filter(Objects::nonNull))使用Optional一致?
我有一个代码
private void processFiles() {
try {
Files.walk(Paths.get(Configurations.SOURCE_PATH))
.filter(new NoDestinationPathFilter()) //<--This one
.filter(new NoMetaFilesOrDirectories()) //<--and this too
.forEach(
path -> {
new FileProcessorFactory().getFileProcessor(
path).process(path);
});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
截至目前,我有各种其他方法,与上述方法相同,只是在滤波器方面有所不同.有些方法有额外的过滤器,有些方法有不同或没有.
是否有可能创建一个条件所需的过滤器集合并动态传入.并且集合中的所有过滤器都应用于流.我不想对正在应用的过滤器列表进行硬编码.我想让它基于配置.我如何实现这一目标?
我在Java 8中有一个异步操作,它返回一个onError回调或onSuccess回调.如果操作成功与否,我需要返回我的方法内部.所以我返回一个布尔值来说明这个信息.我遇到的问题是我得到以下编译错误:
错误:从内部类引用的局部变量必须是最终的或有效的最终
谷歌搜索错误我可以看到你不允许这种类型的操作,但如果操作成功与否,我怎么能返回?
public Boolean addUser(String email, String password) {
Boolean isSuccess = false;
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("email", new AttributeValue(email)); //email
item.put("password", new AttributeValue(password)); //password
dynamoDB.putItemAsync(new PutItemRequest().withTableName("Users").withItem(item), new AsyncHandler() {
@Override
public void onError(Exception excptn) {
}
@Override
public void onSuccess(AmazonWebServiceRequest rqst, Object result) {
isSuccess = true;
}
});
return isSuccess;
}
Run Code Online (Sandbox Code Playgroud) 我有以下for循环:
List<Map> mapList = new ArrayList<>();
for (Resource resource : getResources()) {
for (Method method : resource.getMethods()) {
mapList.add(getMap(resource,method));
}
}
return mapList;
Run Code Online (Sandbox Code Playgroud)
我怎么能将这个嵌套循环重构为Java 8流?
我不敢相信如果没有将类输入到冗余的2类类中,我就无法捕获P:
public class MyClass<T extends List<P>> {
T getList(/**/){}
P getRandomElement(){ /**/ }
}
Run Code Online (Sandbox Code Playgroud)
我是否真的需要定义和实例化MyClass MyClass<String,ArrayList<String>>,因此无法推断它?
编辑:我的意思是我看到冗余必须定义MyClass<P,T extends List<P>>因为那时我需要实例化它总是作为MyClass<String,ArrayList<String>>,并随身携带字符串.如果语言允许这样的话会很好
MyClass<L extends List<P>>或类似的.这样,在执行时和MyClass<ArrayList<String>>返回ArrayList<String>执行时会很好地返回.getList()StringgetRandomElement()
我有两个解决方案,想要一个构成比较器库的枚举:
public enum SongComparator {
BY_TITLE(Comparator.comparing(Song::getTitle)),
BY_ARTIST(Comparator.comparing(Song::getArtist)),
BY_DURATION(Comparator.comparing(Song::getDuration));
private Comparator<Song> comp;
private SongComparator(Comparator<Song> comp) {
this.comp = comp;
}
public Comparator<Song> get() {
return comp;
}
}
Run Code Online (Sandbox Code Playgroud)
和...
public enum SongComparator2 implements Comparator<Song> {
BY_TITLE {
public int compare(Song s1, Song s2) {
return s1.getTitle().compareTo(s2.getTitle());
};
},
BY_ARTIST {
public int compare(Song s1, Song s2) {
return s1.getArtist().compareTo(s2.getArtist());
};
},
BY_DURATION {
public int compare(Song s1, Song s2) {
return Integer.compare(s1.getDuration(), s2.getDuration());
};
};
}
Run Code Online (Sandbox Code Playgroud)
如果我想替换期望Comparator的枚举值,在第一个解决方案中我必须说SongComparator.BY_TITLE.get(); 而在第二个解决方案中我可以说SongComparator2.BY_TITLE.
第二个在这个意义上更好,但是,我不喜欢为每个枚举值编写public int compare …
我如何使用java-8的Stream方法拆分奇数和偶数并在集合中求和?
public class SplitAndSumOddEven {
public static void main(String[] args) {
// Read the input
try (Scanner scanner = new Scanner(System.in)) {
// Read the number of inputs needs to read.
int length = scanner.nextInt();
// Fillup the list of inputs
List<Integer> inputList = new ArrayList<>();
for (int i = 0; i < length; i++) {
inputList.add(scanner.nextInt());
}
// TODO:: operate on inputs and produce output as output map
Map<Boolean, Integer> oddAndEvenSums = inputList.stream(); \\here I want to split odd …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×7
java-stream ×3
lambda ×3
optional ×2
collections ×1
comparator ×1
enums ×1
fxml ×1
generics ×1
interface ×1
javafx-2 ×1
mockito ×1
nullable ×1
types ×1