我看到很多博客文章都提到了Streams类,我发现它曾经是lambda分支API的一部分.它现在似乎是非公共API,它与之前的实现不匹配.是否有不同的方法Streams.concat()或将多个值附加到流?
我正在尝试学习如何在我的日常编程中使用Java 8功能(例如lambdas和流),因为它使代码更清晰.
以下是我目前正在处理的内容:我从本地文件中获取一个字符串流,其中包含一些数据,稍后我将这些数据转换为对象.输入文件结构如下所示:
Airport name; Country; Continent; some number;
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
public class AirportConsumer implements AirportAPI {
List<Airport> airports = new ArrayList<Airport>();
@Override
public Stream<Airport> getAirports() {
Stream<String> stream = null;
try {
stream = Files.lines(Paths.get("resources/planes.txt"));
stream.forEach(line -> createAirport(line));
} catch (IOException e) {
e.printStackTrace();
}
return airports.stream();
}
public void createAirport(String line) {
String airport, country, continent;
int length;
airport = line.substring(0, line.indexOf(';')).trim();
line = line.replace(airport + ";", "");
country = line.substring(0,line.indexOf(';')).trim();
line = line.replace(country + ";", "");
continent = …Run Code Online (Sandbox Code Playgroud) 我想弄清楚为什么这段代码不能在JDK上编译1.8.0_45:
public class Example<E extends Example<E>> {
public List<? extends Example<?>> toExamples(Collection<String> collection) {
return collection.stream()
.map(v -> lookup(v))
.collect(Collectors.toList());
}
public static <E extends Example<E>> E lookup(String value) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
添加一个看似不必要的演员修复了它:
public class Example<E extends Example<E>> {
public List<? extends Example<?>> toExamples(Collection<String> collection) {
return collection.stream()
.map(v -> (Example<?>) lookup(v))
.collect(Collectors.toList());
}
public static <E extends Example<E>> E lookup(String value) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这是编译器的错误:
Example.java:9: error: incompatible types: inference variable R has …Run Code Online (Sandbox Code Playgroud) 我无法解释这一点,但我在其他人的代码中发现了这种现象:
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.stream.Stream;
import org.junit.Test;
public class TestDidWeBreakJavaAgain
{
@Test
public void testIoInSerialStream()
{
doTest(false);
}
@Test
public void testIoInParallelStream()
{
doTest(true);
}
private void doTest(boolean parallel)
{
Stream<String> stream = Stream.of("1", "2", "3");
if (parallel)
{
stream = stream.parallel();
}
stream.forEach(name -> {
try
{
Files.createTempFile(name, ".dat");
}
catch (IOException e)
{
throw new UncheckedIOException("Failed to create temp file", e);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在启用安全管理器的情况下运行时,仅仅调用parallel()流,或者parallelStream()从集合中获取流时,似乎可以保证所有执行I/O的尝试都会抛出SecurityException.(最有可能的,它调用任何方法可以抛出 …
如何使用java 8添加所有内容?
processeditemList is a Map<Integer, Map<Item, Boolean>>
Run Code Online (Sandbox Code Playgroud)
至于现在我在做:
List<Item> itemList = Lists.newLinkedList();
for (Map<Item, Boolean> entry : processeditemList.values()) {
itemList.addAll(entry.keySet());
}
return itemList;
Run Code Online (Sandbox Code Playgroud) 我对以下场景有疑问;
我有一个n-number数组ArrayList.数组的数量不是预先确定的,而数组的大小是固定的,但它可以包含空元素,空值被认为是零.我需要将每个单元格的总和放入新数组中的相应索引中,其大小在逻辑上与其他数组相同.
我试图将场景描述如下;
我有一个经典的迭代方法的解决方案,但我最终得到了一个非常脏的实现.我很乐意看到你解决这个问题(最好是Stream api)
我正在寻找一种聪明的方法来执行以下操作:
列出一个数字列表:
1,2,3,4,5,12,13,14,19
并将其压缩成一个字符串,如下所示:
1-5,12-14,19
使用以下规则:当范围中的数字计数为3或更多时,仅压缩到范围(即使用破折号).
即:1,2,4,5将导致:1,2,4,5和NOT:1-2,4-5
为什么Java的没有选择这个签名<T> Stream <T> Stream.generate (Supplier <? extends T> supplier)在这一个<T> Stream <T> Stream.generate (Supplier <T> supplier)?
我的意思是下面的例子(不编译)是正确的,因为字符串的供应商在一个密码序列流中也是有效的吗?
Supplier <String> constantHello = () -> "Hello";
long count = Stream.<CharSequence>generate(constantHello).count();
Run Code Online (Sandbox Code Playgroud) 我有一个名为"Book"的数据结构,它包含以下字段:
public final class Book {
private final String title;
private final BookType bookType;
private final List<Author> authors;
}
Run Code Online (Sandbox Code Playgroud)
我的目标是Map<Author, List<BookType>>从List<Book>使用Stream API 派生出来.
为了实现它,我首先制作了一个for-each循环来阐明解决方案的步骤,并在我将其重新编写为基于流的方法后逐步完成:
Map<Author, List<BookType>> authorListBookType = new HashMap<>();
books.stream().forEach(b -> b.getAuthors().stream().forEach(e -> {
if (authorListBookType.containsKey(e)) {
authorListBookType.get(e).add(b.getBookType());
} else {
authorListBookType.put(e, new ArrayList<>(Collections.singletonList(b.getBookType())));
}
}));
Run Code Online (Sandbox Code Playgroud)
但它不是基于Stream API的解决方案,而且我已陷入困境,我不知道如何正确完成它.我知道我必须使用分组收集器Map<Author, List<BookType>>从流中获取所需的直接.
你能给我一些提示吗?
我想通过一个可空字段对对象列表进行排序.
为了避免NullPointerexception我使用Comparator.nullsLast.但例外情况仍然存在:
public class Test {
public static void main(String[] args) {
List<Bean> l = new ArrayList<>();
for (int i=0;i<5;i++) {
Bean b = new Bean("name_"+i,i);
l.add(b);
}
l.get(2).setVal(null);
System.out.println(l);
Collections.sort(l, Comparator.nullsLast(Comparator.comparing(Bean::getVal)));
System.out.println(l);
}
static class Bean{
String name;
Integer val;
// omit getters & setters & constructor
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能对这种清单进行排序?
java ×10
java-stream ×10
java-8 ×8
lambda ×3
arrays ×2
collections ×1
comparator ×1
generics ×1
range ×1