如何使用 Java 8 lambda/stream() 以更简短、更直观的方式在函数式编程中编写此方法?
int animation = R.style.DialogAnimationSlideHorizontal;
String body;
String subject = mSubjectView.getText().toString();
BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL);
if (!checkFields()) {
// one of the recipients is invalid.
body = getString(R.string.bad_address);
dialog.showDialog(body, animation, new DialogBuilder.Positive() {
@Override
public void handleClick(DialogInterface dialogInterface, View view) {
// do nothing
}
});
} else if (Helpers.isEmpty(subject)) {
// Yup, empty... send the message without a subject?
body = getString(R.string.empty_subject_compose);
dialog.showDialog(body, animation, new DialogBuilder.Positive() {
@Override
public void handleClick(DialogInterface dialogInterface, View view) {
// user accepted to send anyway.
mWebView.getComposeContent();
}
});
} else {
// everything is correct! send the message.
mWebView.getComposeContent();
}
最佳答案
用 lambda 替换所有匿名类,例如
new DialogBuilder.Positive() {
@Override
public void handleClick(DialogInterface dialogInterface, View view) {
...
}
}
|
V
(dialogInterface, view) -> { /*do nothing*/ }
(dialogInterface, view) -> { mWebView.getComposeContent(); }
而且我不知道如何在这里应用 Stream API。
关于java - 将方法转换为 java 8 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438097/