我们都知道Optional<T>
有一种方法T get()
,为什么它没有实现Supplier<T>
呢?
如果碰巧没有理由,如果Oracle要将它实现到Java的未来版本中会不会破坏任何先前的代码?
我正在使用 JShell 来测试一个库,我通过类路径为其提供了类、源和 javadoc,如下所示:
jshell --class-path library-javadoc.jar:library-sources.jar:library-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)
尽管如此,当我收到记录的 Java 标识符之后的双选项卡时:
<no documentation found>
Run Code Online (Sandbox Code Playgroud)
JShell 对文档有何期待?
嘿,如果有人有想法,我会非常感激.我在Java流中,我想排序我将要返回的列表.我需要通过TradPrefis(MyObject :: getTradPrefix)对列表进行排序.但这太简单了.因为我想按照TradPrefix exampleTradPrefix_ [NUMBER TO SORT]末尾的数字排序
例如:hello_1 test_2 ... still_there_22
这是一段代码,您可以更容易想象.
public LinkedHashSet<WsQuestion> get(String quizId, String companyId) {
LinkedHashSet<QuizQuestionWithQuestion> toReturn = quizQuestionRepository.findAllQuizQuestionWithQuestionByQuizId(quizId);
return (toReturn.stream()
.map(this::createWsQuestion)
.sorted(comparing(WsQuestion::getTradPrefix.toString().length()))
.collect(Collectors.toCollection(LinkedHashSet::new)));
}
Run Code Online (Sandbox Code Playgroud) 我有一个对象列表。我想做一个 groupBy 以便有:
和一个不是上述任何一个的组。
我可以:
for (Object obj: myList) {
if (obj instanceof Long) || (obj instanceof String) {
// add to sublist1
} else if (obj instanceof Map) {
// sublist2
} else {
// sublist3
}
}
Run Code Online (Sandbox Code Playgroud)
我如何使用 Java 8?
public enum Dictionaries {
FIRST_DICTIONARY(FirstDictionary.class),
SECOND_DICTIONARY(SecondDictionary.class);
private Class<? extends DictionaryModel> clazz;
private Dictionary(Class<? extends DictionaryModel> clazz) {
this.clazz = clazz;
}
public Class<? extends DictionaryModel> clazz() {
return this.clazz;
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个枚举。FirstDictionary
并SecondDictionary
实现DictionaryModel
,这是为了使用此解决方案的标记接口。
现在我很想这样做:
Class<FirstDictionary> clazz = Dictionaries.FIRST_DICTIONARY.clazz();
这种设计显然是不可能的,我想不出实现这一目标的方法。有没有办法做到这一点?我可以访问整个代码,因此我可以修改所有内容,包括界面(甚至删除它)。
我简化了所有这一切,重点是每个字典都是一个数据库表,我有一个通用的 DAO(而不是每个字典的 DAO),现在我必须转换我想避免的每次读取的结果。我知道通用 DAO 不是一个好主意(或者根本不是 DAO)。
Optional<DictionaryModel> entity = dao.getByValue(Dictionaries.FIRST_DICTIONARY, value);
即使对于任何动态转换,在 DAO 本身或clazz()
方法中,我似乎也无法解决。我觉得我在这里遗漏了一些明显的东西。
我很欣赏任何想法,甚至完全改变设计。
作为练习,我将一些旧代码转换为功能流。我对流了解不多。似乎转换此代码应该很简单,但是我运气不高。该方法从给定的整数开始,将其传递给isPrime,如果为素数则返回true。然后递出要打印的新(下一个)素数。如果isPrime为false,则我递增,然后检查下一个整数。
private static int nextPrime(final int number) {
int i = number + 1;
while (!isPrime(i)) {
i++;
}
return i;
}
Run Code Online (Sandbox Code Playgroud) Java 10的发布带来了新的静态工厂方法,特别是:
static <E> List<E> copyOf?(Collection<? extends E> coll)
static <E> Set<E> copyOf?(Collection<? extends E> coll)
static <K,V> Map<K,V> copyOf?(Map<? extends K,? extends V> map)
看到这些方法允许我们将Collection
s 复制到不同的Collection
实现中,它们如何与现有方法进行比较和对比?
我记得在某处读过带有推断类型的局部变量可以用相同类型的值重新分配,这是有意义的.
var x = 5;
x = 1; // Should compile, no?
Run Code Online (Sandbox Code Playgroud)
但是,我很好奇如果要重新分配x
给不同类型的对象会发生什么.这样的东西还会编译吗?
var x = 5;
x = new Scanner(System.in); // What happens?
Run Code Online (Sandbox Code Playgroud)
我目前无法安装JDK 10的早期版本,并且不想等到明天才能找到答案.
我尝试在堆栈窗格中创建一个包含 2 个窗格的应用程序。一个窗格是主窗格并居中,第二个较小并停靠在舞台的左下角。
问题是我尝试过使用“setAlignment”,但它似乎不起作用(尽管按钮已对齐)。小窗格始终居中。
有什么问题,我如何解决这个问题?我想也许我无法对齐窗格,那么我该如何克服呢?
Pane pane = new Pane();
for (SerialPoint sp : points) {
Circle circle = new Circle(sp.getX(), sp.getY(), 6, Color.GREEN);
pane.getChildren().add(circle);
}
Pane smallPane = new Pane();
smallPane.setScaleX(0.25);
smallPane.setScaleY(0.25);
smallPane.setStyle("-fx-border-color: black;");
for (SerialPoint sp : points) {
Circle circle = new Circle(sp.getX(), sp.getY(), 6, Color.RED);
smallPane.getChildren().add(circle);
}
Button startBtn = new Button("Start");
StackPane stackPane = new StackPane(pane, smallPane, startBtn);
StackPane.setAlignment(smallPane, Pos.BOTTOM_LEFT);
StackPane.setAlignment(startBtn, Pos.TOP_RIGHT);
StackPane.setMargin(startBtn, new Insets(5));
Scene scene = new Scene(stackPane);
Run Code Online (Sandbox Code Playgroud)
(SerialPoint 是我的内部类)
假设我有以下类定义单个static
实用程序方法:
import java.io.IOException;
import java.nio.channels.AsynchronousSocketChannel;
public class Utility {
public static AsynchronousSocketChannel getChannel() {
try {
return AsynchronousSocketChannel.open();
} catch (IOException e) {
throw new IllegalStateException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以创建一个Utility
使用此方法的类(位于相同的包中):
public class Test {
public static void main(String[] args) throws Exception {
var channel = Utility.getChannel();
System.out.println(channel);
channel.close();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,Test
似乎不需要任何import语句,即使它AsynchronousSocketChannel
在本地使用.如果我要输入AsynchronousSocketChannel channel = ...;
,那么显然需要import语句.
我的假设是在编译时推断导入语句(当利用本地类型推断时)是正确的吗?