早期jdk8-ea的javadoc 这样表示有一个java.util.stream.Streamable接口,它似乎有一个同样的关系Stream作为Iterable必须的Iterator.
现在好像我们被困住了Supplier<Stream>,这肯定是不一样的.
发生什么事了Streamable?
情况
我有一个OSGi项目,我正在尝试迁移到Java 8.在我的项目中,我依赖于第三方库,我"OSGi-fied"(通过添加MANIFEST.MF文件并将元数据放入其中).这些库是从只读SVN存储库中检出的,所以我只需要在需要时检查更新,因此我不想进行任何其他更改,而不是在MANIFEST.MF文件中,因为我无法提交它们.
问题
但是,这些库使用了许多匿名比较器,如:
private static final Comparator heightComparator = new Comparator() {
public int compare (Object o1, Object o2) {
return ((Glyph)o1).getHeight() - ((Glyph)o2).getHeight();
}
};
Run Code Online (Sandbox Code Playgroud)
现在,显然java.util.Comparator接口有一大堆需要实现的新方法(当然,这会导致编译错误).但我真的想避免实现它们或切换到Lambda表达式,因为修改原始源很可能每次检查更新版本时都会导致冲突.
Java曾经在向后兼容性方面努力工作,我想知道为什么这么简单且广泛使用的API部分需要(相对)大量迁移.我错过了什么或是否真的不可避免?
我知道出于并发原因,我无法在Java 8中更新lambda中局部变量的值.所以这是非法的:
double d = 0;
orders.forEach( (o) -> {
d+= o.getTotal();
});
Run Code Online (Sandbox Code Playgroud)
但是,如何更新实例变量或更改本地对象的状态?例如,Swing应用程序我有一个按钮和一个声明为实例变量的标签,当我点击按钮时我想要隐藏标签
jButton1.addActionListener(( e) -> {
jLabel.setVisible(false);
});
Run Code Online (Sandbox Code Playgroud)
我没有编译器错误并且工作正常,但是...改变lambda中对象的状态是正确的吗?我将来会遇到并发问题还是坏事?
这是另一个例子.想象一下,下面的代码是在一个servlet的方法doGet中我会在这里遇到一些问题吗?如果答案是肯定的:为什么?
String key = request.getParameter("key");
Map<String, String> resultMap = new HashMap<>();
Map<String, String> map = new HashMap<>();
//Load map
map.forEach((k, v) -> {
if (k.equals(key)) {
resultMap.put(k, v);
}
});
response.getWriter().print(resultMap);
Run Code Online (Sandbox Code Playgroud)
我想知道的是:什么时候改变lambda中对象实例的状态是正确的?
我的目标:采取LinkedList的UserS和提取LinkedList在一个优雅的,Java的8路的用户名.
public static void main(String[] args) {
LinkedList<User> users = new LinkedList<>();
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
// Vanilla Java approach
LinkedList<String> usernames = new LinkedList<>();
for(User user : users) {
System.out.println(user.getUsername());
usernames.add(user.getUsername());
}
System.out.println("Usernames = " + usernames.toString());
// Java 8 approach
users.forEach((user) -> System.out.println(user.getUsername()));
LinkedList<String> usernames2 = users.stream().map(User::getUsername). // Is there a way to turn this map into a LinkedList?
System.out.println("Usernames = " + usernames2.toString());
}
static class …Run Code Online (Sandbox Code Playgroud) 我正在玩Java 8可完成的期货.我有以下代码:
CountDownLatch waitLatch = new CountDownLatch(1);
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
try {
System.out.println("Wait");
waitLatch.await(); //cancel should interrupt
System.out.println("Done");
} catch (InterruptedException e) {
System.out.println("Interrupted");
throw new RuntimeException(e);
}
});
sleep(10); //give it some time to start (ugly, but works)
future.cancel(true);
System.out.println("Cancel called");
assertTrue(future.isCancelled());
assertTrue(future.isDone());
sleep(100); //give it some time to finish
Run Code Online (Sandbox Code Playgroud)
使用runAsync我计划执行等待锁存器的代码.接下来我取消了未来,期望被抛入中断的异常.但似乎线程在await调用上仍然被阻塞,即使未来被取消(断言传递),也不会抛出InterruptedException.使用ExecutorService的等效代码按预期工作.它是CompletableFuture中的错误还是我的示例中的错误?
我正在尝试启动Cassandra,我遇到了一个问题,JavaLaunchHelper位于两个地方.我正在运行Java 8.这是确切的错误:
objc[413]: Class JavaLaunchHelper is implemented in both
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java and
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/libinstrument.dylib.
One of the two will be used. Which one is undefined.
Run Code Online (Sandbox Code Playgroud)
为Java 7找到了类似的问题:
类JavaLaunchHelper在两者中实现.将使用两者之一.哪一个未定义
解决这个问题的最佳方法是什么?
请考虑以下代码片段
String strings[] = {"test"};
final List<String> collect = java.util.Arrays.stream(strings).collect(java.util.stream.Collectors.toList());
final Double[] array = java.util.Arrays.stream(strings).toArray(Double[]::new);
Run Code Online (Sandbox Code Playgroud)
Java的为什么能保证在收集情况的正确类型(改变泛型类型的收集到例如双导致编译时错误),但不是在阵列情况下(编译罚款,尽管apply(int)中Double[]::new给出了一个Double[],而不是一个Object[],而是抛出ArrayStoreException如果上面使用不正确)?
什么是不改变给定生成的情况下,编译时错误的最好方式,我改变流的类型IntFunction的toArray电话吗?
我想换一个java.util.streams.Stream围绕InputStream处理一个字节或一次一个字符.我没有找到任何简单的方法来做到这一点.
请考虑以下练习:我们希望计算每个字母在文本文件中出现的次数.我们可以将它存储在一个数组中,以便tally[0]存储在文件中出现的次数,tally[1]存储出现的时间b等等.由于我找不到直接传输文件的方法,我这样做了:
int[] tally = new int[26];
Stream<String> lines = Files.lines(Path.get(aFile)).map(s -> s.toLowerCase());
Consumer<String> charCount = new Consumer<String>() {
public void accept(String t) {
for(int i=0; i<t.length(); i++)
if(Character.isLetter(t.charAt(i) )
tall[t.charAt(i) - 'a' ]++;
}
};
lines.forEach(charCount);
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用该lines方法的情况下完成此操作?我可以直接将每个字符作为Stream或Stream处理,而不是为文本文件中的每一行创建字符串.
我可以更直接地转换java.io.InputStream成java.util.Stream.stream?
考虑一下java 8代码片段:
public class Generics {
public static <V, E extends Exception> V f(CheckedCallable1<V, E> callable) throws E {
return callable.call();
}
public static <V, E extends Exception> V g(CheckedCallable2<V, E> callable) throws E {
return callable.call();
}
public static void main(String[] args) {
f(() -> 1);
g(() -> 1);
}
}
interface Callable<V> {
V call() throws Exception;
}
interface CheckedCallable1<V, E extends Exception> {
V call() throws E;
}
interface CheckedCallable2<V, E extends Exception> extends Callable<V> {
@Override …Run Code Online (Sandbox Code Playgroud)