我试图创建两个列表 - 赔率和均衡如下:
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 5, 8, 13, 21));
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
numbers.stream().forEach(x -> x % 2 == 0 ? evens.add(x) : odds.add(x));
}
Run Code Online (Sandbox Code Playgroud)
但它给了我不兼容的类型错误(lambda表达式缺少返回值的错误返回类型)
将集合过滤到两个新集合的最佳方法是什么?
似乎ResultSet空的标准检查在Spring的JDBC中不起作用ResultSetExtractor。我通过以下方式使用它:
return jdbcTemplate.query(sql, ps -> {
ps.setLong(1, fooParam1);
ps.setLong(2, fooParam2);
}, rs -> {
return rs.getLong(1);
});
Run Code Online (Sandbox Code Playgroud)
那么ResultSet在这种模式下检查空度的正确方法是什么?
如何使用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 …Run Code Online (Sandbox Code Playgroud) 我有一个Map流我怎么能得到具有Map值的Set?
我在这里部分地做了什么
Set<String> jcfTargetTables = measure.getConditionMap().values()
.stream()
.map(Condition::getJoinConditionFilter)
.filter(jcf -> jcf!=null)
.map(JoinConditionFilter::getTableMapping);
Run Code Online (Sandbox Code Playgroud)
最后一行给了我一个Stream<Map<String,String>>,我如何继续获取Set是Map的值?
使用java.time API,可以使用以下语法比较两个日期:
date1.before(date2);
Run Code Online (Sandbox Code Playgroud)
要么
date2.after(date1);
Run Code Online (Sandbox Code Playgroud)
然而,当比较3个或更多日期时,这变得非常冗长.有没有办法比较Date和其他多个,看看提供的是在所有其他的之前/之后?
声纳提到,这个java代码应该与lambda一起使用,但我从未使用过lamdas,也不知道如何使用它.有人能指出我这个代码的正确版本:?
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
closeable.close();
logger.info("Close closeable.");
executorPool.shutdown();
logger.info("Shutdown executorPool");
}
}));
Run Code Online (Sandbox Code Playgroud) 通过拖动拖放区中的列,默认Javafx表可以对多个字段进行排序吗?
我的用户需要选择一个或多个列以对不同的列进行排序。该应用程序完全使用Java8和JavaFX编写。
我现在使用的源代码是:
import java.util.Collections;
import java.util.Comparator;
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class GroupByTable extends Application {
public enum Color { GREEN, BLUE, RED }
public enum Shape { RECTANGLE, CIRCLE, TRIANGLE }
public enum Size { SMALL, MEDIUM, LARGE }
private Label groupByLabel;
private Comparator<Item> groupingComparator ;
@Override
public void start(Stage primaryStage) {
TableView<Item> table …Run Code Online (Sandbox Code Playgroud) 我有一个Java 8应用程序,我在一个线程上运行一个任务:
Thread t = new Thread(() -> { runTask();
finalizeTask(); });
t.start()
saveThread(t);
Run Code Online (Sandbox Code Playgroud)
在某些时候,从不同的线程,这个任务可能会被中断:
Thread t = getThread(); // Obtains thread t
t.interrupt();
Run Code Online (Sandbox Code Playgroud)
有了这个,我希望runTask();线路被中断,但我还是喜欢finalizeTask();运行.
如何捕获线程中断并处理它?
我想转换一个,Integer[]但是toArray()给了我Object[].我该怎么把它转换成String[]?
Stream.of(ids).map(String::valueOf).toArray();
Run Code Online (Sandbox Code Playgroud) 我有这样的数据:
List<Map<String, String>> = // psuedocode
[
{
"ID": "a",
"Value": "val1"
},
{
"ID": "b",
"Value": "val2"
},
]
Run Code Online (Sandbox Code Playgroud)
我想把它变成
Map<String, String> = // more pseudocode
{
"a": "val1",
"b": "val2"
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这样的事情:
Map<String, String> output = myList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap())
Run Code Online (Sandbox Code Playgroud)
有人可以指导我吗?另一种方法是使用常规for循环,但我正在尝试使用lambdas.