是否可以Enumeration使用Lambda Expression 进行迭代?以下代码片段的Lambda表示形式是什么:
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
while (nets.hasMoreElements()) {
NetworkInterface networkInterface = nets.nextElement();
}
Run Code Online (Sandbox Code Playgroud)
我没有在其中找到任何流.
我试图列出整数列表中的重复元素,例如,
List<Integer> numbers = Arrays.asList(new Integer[]{1,2,1,3,4,4});
Run Code Online (Sandbox Code Playgroud)
使用jdk的Streams 8.有没有人试过.要删除重复项,我们可以使用distinct()api.但是如何找到重复的元素呢?有人可以帮帮我吗?
我创建一个Map从List如下:
List<String> strings = Arrays.asList("a", "bb", "ccc");
Map<String, Integer> map = strings.stream()
.collect(Collectors.toMap(Function.identity(), String::length));
Run Code Online (Sandbox Code Playgroud)
我希望保持与之相同的迭代顺序List.如何创建LinkedHashMap使用Collectors.toMap()方法?
我有以下示例代码:
System.out.println(
"Result: " +
Stream.of(1, 2, 3)
.filter(i -> {
System.out.println(i);
return true;
})
.findFirst()
.get()
);
System.out.println("-----------");
System.out.println(
"Result: " +
Stream.of(1, 2, 3)
.flatMap(i -> Stream.of(i - 1, i, i + 1))
.flatMap(i -> Stream.of(i - 1, i, i + 1))
.filter(i -> {
System.out.println(i);
return true;
})
.findFirst()
.get()
);
Run Code Online (Sandbox Code Playgroud)
输出如下:
1
Result: 1
-----------
-1
0
1
0
1
2
1
2
3
Result: -1
Run Code Online (Sandbox Code Playgroud)
从这里我看到,在第一种情况下stream真的表现得懒惰 - 我们使用findFirst()所以一旦我们有第一个元素我们的过滤lambda没有被调用.然而,在使用flatMaps的第二种情况下,我们看到尽管找到满足过滤条件的第一个元素(它只是任何第一个元素,因为lambda总是返回true),流的其他内容仍然通过过滤函数被馈送.
我试图理解为什么它表现得像这样,而不是在第一个元素计算后放弃,如第一种情况.任何有用的信息将不胜感激.
我注意到在执行中有些奇怪HashMap.clear().这就是它在OpenJDK 7u40中的表现:
public void clear() {
modCount++;
Arrays.fill(table, null);
size = 0;
}
Run Code Online (Sandbox Code Playgroud)
这就是OpenJDK 8u40的外观:
public void clear() {
Node<K,V>[] tab;
modCount++;
if ((tab = table) != null && size > 0) {
size = 0;
for (int i = 0; i < tab.length; ++i)
tab[i] = null;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道现在table为空地图可以为null,因此需要在局部变量中进行额外的检查和缓存.但为什么被Arrays.fill()for-loop取代?
似乎在此提交中引入了更改.不幸的是,我没有找到解释为什么普通for循环可能比更好Arrays.fill().它更快吗?还是更安全?
在尝试多捕获功能时,我在我的m1()方法中发现一切都按预期正常工作。
但是,在m2()相同的代码中不能编译。我刚刚更改了语法以减少代码行数。
public class Main {
public int m1(boolean bool) {
try {
if (bool) {
throw new Excep1();
}
throw new Excep2();
//This m1() is compiling abs fine.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
public int m2(boolean b) {
try {
throw b ? new Excep1() : new Excep2();
//This one is not compiling.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
private static interface …Run Code Online (Sandbox Code Playgroud) 我正在使用Java 8中的新lambda特性,并发现Java 8提供的实践非常有用.但是,我想知道是否有一种很好的方法可以解决以下情况.假设您有一个对象池包装器,需要某种工厂来填充对象池,例如(使用java.lang.functions.Factory):
public class JdbcConnectionPool extends ObjectPool<Connection> {
public ConnectionPool(int maxConnections, String url) {
super(new Factory<Connection>() {
@Override
public Connection make() {
try {
return DriverManager.getConnection(url);
} catch ( SQLException ex ) {
throw new RuntimeException(ex);
}
}
}, maxConnections);
}
}
Run Code Online (Sandbox Code Playgroud)
将函数接口转换为lambda表达式后,上面的代码变为:
public class JdbcConnectionPool extends ObjectPool<Connection> {
public ConnectionPool(int maxConnections, String url) {
super(() -> {
try {
return DriverManager.getConnection(url);
} catch ( SQLException ex ) {
throw new RuntimeException(ex);
}
}, maxConnections); …Run Code Online (Sandbox Code Playgroud) 就像java.util.Optional<T>在Java 8中(有点)等同于Scala的Option[T]类型,是否有相当于Scala的Either[L, R]?
在lambda中,局部变量需要是final,但实例变量不需要.为什么这样?
在Java8流中,我可以修改/更新其中的对象吗?例如.List<User> users:
users.stream().forEach(u -> u.setProperty("value"))
Run Code Online (Sandbox Code Playgroud) java ×10
java-8 ×10
lambda ×5
java-stream ×4
arrays ×1
collections ×1
enumeration ×1
exception ×1
final ×1
hashmap ×1
iteration ×1
scala ×1
try-catch ×1