如何在菜单项组之间插入分隔线。例如:
第 1 组:保存、另存为
第 2 组打开导入
我的技巧是在中间添加一个禁用的菜单项,字符串值为“---------”。所以我现在的菜单是这样的
“节省”
“另存为”
“——”
“打开”
“进口”
有没有更合适、更优雅的方法来做到这一点?
在Comparable的契约中,没有任何东西强迫一个对象与它自己相比较。只是
强烈推荐,但不严格要求 (x.compareTo(y)==0) == (x.equals(y))
这意味着,它的建议对于x.compareTo(x)不扔。但是可以写一个
class X implements Comparable<Y> {
...
}
Run Code Online (Sandbox Code Playgroud)
其中X和Y是两个不相关的类。我看不出它有什么好处,但在 Java 8 版本中HashMap甚至有相应的检查。
X implements Comparable<Y>使用两个不相关的类来实现?我想答案是肯定的和否定的,但这只是一个猜测
在 JavaFX 2.2 中,我在导入具有 FXML 中定义的自定义单元工厂的自定义组件时遇到问题。假设我的自定义组件如下
public class CustomComponent extends VBox{
public CustomComponent() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomComponent.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch (IOException e ){
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
}
而对应的 FXML 是
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import application.*?>
<?import application.TestFactory?>
<fx:root prefHeight="358.0" prefWidth="260.0" type="VBox" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<TableView prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="C1" >
<cellFactory>
<TestFactory />
</cellFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="C2" >
<cellFactory>
<TestFactory …Run Code Online (Sandbox Code Playgroud) 假设我有一个 C 类的对象 c(来自一个库,所以我不能修改类定义)它扩展了 Iterable。
在我的函数中,我想为用户提供一种处理类型 R 对象流的方法,其中类型 R 的对象是通过对类型 T 的对象进行转换获得的。
目前我执行以下操作:
Stream<R> f(...) {
C c = ...;
return StreamSupport.stream(c.spliterator(), false)
.map(...);
}
Run Code Online (Sandbox Code Playgroud)
它之所以有效是因为 spliterator 在类 Iterable 中默认实现,但 javadoc 说出于性能原因不建议使用默认实现。
如果用户只想处理或应用流操作,我想返回一个流而不创建列表。我对流的并行化属性不感兴趣,因为 c 中的对象只能以顺序方式读取。
所以我想知道,如果有的话,做这种事情的推荐方式是什么。简单地返回一个 List 还是传递一个消费者函数作为参数更好?我有点担心建议不要使用默认拆分器实现的 javadoc。使用上述方法,我可以确保保留底层 Stream 中元素出现的顺序吗?
我有一个流并行执行一系列操作,然后我必须将结果写入文件,所以我需要写入操作是顺序的,但是它需要作为一个流来执行,我有很多数据要写入,我不能使用中间集合。有没有办法做到这一点?
我想到了一个看起来不太干净的解决方案,那就是让写法同步。这种方法是唯一可能的吗?还有其他方法吗?
谢谢你。
我对 Java 8 中的流很陌生,所以我的方法可能是错误的。
我有 2 个对象如下
object1 {
BigDecimal amount;
Code1 code1;
Code2 code2;
Code3 code3;
String desc;
}
object2 {
BigDecimal amount;
Code1 code1;
Code2 code2;
Code3 code3;
}
Run Code Online (Sandbox Code Playgroud)
所以我想收集 code1 && code2 && code3 相同的所有 object1,然后将数量相加将其添加到 object2 列表中。
我没有代码来做到这一点......我想编写一个代码来完成这项工作我正在尝试从http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html实现一些东西
或者按部门计算所有工资的总和:
// Compute sum of salaries by department
Map<Department, Integer> totalByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
Run Code Online (Sandbox Code Playgroud) 假设我有员工数据列表。
class Employee {
private long id;
private String name;
private int age;
// Constrctor
// setters & getters
}
List<Employee> empList = ...;
Run Code Online (Sandbox Code Playgroud)
使用上面的empList,如何使用Java8流将年龄> 20的两个不同列表分成一个列表,将年龄<20的两个列表分成一个列表。
我试图在Integer流上使用reduce方法来获取所有值的总和。但出现语法错误,无法找出错误。
List<Integer> ee = new ArrayList<Integer>();
Function<? super Integer, ? extends Integer> f3 = x -> x / 2;
BinaryOperator<? extends Integer> accumulator = (x, y) -> x + y;
ee.stream().map(f3).reduce(new Integer(0), accumulator);
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
The method reduce(capture#2-of ? extends Integer, BinaryOperator<capture#2-of ? extends Integer>) in the type Stream<capture#2-of ? extends Integer> is not applicable for the arguments (Integer, BinaryOperator<capture#7-of ? extends Integer>)
Run Code Online (Sandbox Code Playgroud) 在以下代码段中,我的Foo类带有始终返回的相同实例的方法Optional。然后我有另一个OneMoreClass使用Foo类的类。
您可以从源代码中看到调用get()方法是安全的,method1因为始终在中检查该方法method2。
IntelliJ IDEA仍然显示警告的问题(您只需将此代码段复制到IDEA,您将看到此问题)。
public class Example {
public static class Foo {
private final Optional<String> value = Optional.empty();
public Optional<String> bar() {
return value;
}
}
public static class OneMoreClass {
void method1(final Foo foo) {
method2(foo);
System.out.println(foo.bar().get()); // here the warning is shown in IntelliJ IDEA:
// "Optional.get()" without "isPresent()" check
}
void method2(final Foo foo) {
if (!foo.bar().isPresent()) {
throw new IllegalArgumentException("No value"); …Run Code Online (Sandbox Code Playgroud) 我有一个ints类似以下列表:
1318 1065 0
1392 1109 0
1522 1114 2
1764 1134 0
1643 1172 0
1611 1141 0
1608 1142 4
1689 1180 0
1546 1144 0
1811 1121 1
1682 1144 0
1687 1203 0
1751 1138 0
1702 1227 0
Run Code Online (Sandbox Code Playgroud)
我的目标是仅保留第三个元素为0的条目。
我已经尝试过一些方法,例如:
for( int i=0; i<data_fin.size();i++) {
data_fin.removeIf(s -> !((data_fin.get(i)[2]) == 0));
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息“我在封闭范围内定义的局部变量必须是最终的或实际上是最终的”。
谁能帮助我了解我在做什么错?我是Java的新手,可能使用的是removeif错误的,所以我将不胜感激!
java-8 ×10
java ×7
java-stream ×4
collectors ×1
comparable ×1
dictionary ×1
grouping ×1
int ×1
javafx ×1
javafx-2 ×1
javafx-8 ×1
menu ×1
optional ×1
remove-if ×1
scenebuilder ×1