与Java-8 I可以很容易地处理一个String(或任何CharSequence),为IntStream使用任一chars或codePoints方法.
IntStream chars = "Hello world.".codePoints();
Run Code Online (Sandbox Code Playgroud)
然后我可以操纵流的内容
IntStream stars = chars.map(c -> c == ' ' ? ' ': '*');
Run Code Online (Sandbox Code Playgroud)
我一直在寻找一种整洁的方式来打印结果,我甚至找不到一个简单的方法.如何将这个ints流放回一个可以像我一样打印的形式String.
从上面stars我希望打印
***** ******
Run Code Online (Sandbox Code Playgroud) 我有以下内容Stream:
Stream<T> stream = stream();
T result = stream.filter(t -> {
double x = getX(t);
double y = getY(t);
return (x == tx && y == ty);
}).findFirst().get();
return result;
Run Code Online (Sandbox Code Playgroud)
但是,并不总是有一个结果给我以下错误:
NoSuchElementException:没有值存在
那么null如果没有价值,我怎么能回来?
我刚刚阅读了有关Branch-Prediction的内容,并想尝试使用Java 8 Streams.
然而,Streams的性能总是比传统的循环更差.
int totalSize = 32768;
int filterValue = 1280;
int[] array = new int[totalSize];
Random rnd = new Random(0);
int loopCount = 10000;
for (int i = 0; i < totalSize; i++) {
// array[i] = rnd.nextInt() % 2560; // Unsorted Data
array[i] = i; // Sorted Data
}
long start = System.nanoTime();
long sum = 0;
for (int j = 0; j < loopCount; j++) {
for (int c = 0; c < totalSize; …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的列表:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Run Code Online (Sandbox Code Playgroud)
是否可以使用Java 8流从此列表中获取每个第二个元素以获取以下内容?
[1, 3, 5, 7, 9]
Run Code Online (Sandbox Code Playgroud)
或者甚至每三个元素?
[1, 4, 7, 10]
Run Code Online (Sandbox Code Playgroud)
基本上,我正在寻找一个函数来获取流的每个第n个元素:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> list2 = list.stream().takenth(3).collect(Collectors.toList());
System.out.println(list2);
// => [1, 4, 7, 10]
Run Code Online (Sandbox Code Playgroud) 我有几张包含大量数据的表(大约1亿条记录).所以我不能将这些数据存储在内存中,但是我希望使用类来传输这个结果集java.util.stream并将此流传递给另一个类.我读了关于Stream.of和Stream.Builder运算符,但它们是内存中的缓冲流.那么有什么方法可以解决这个问题吗?提前致谢.
更新#1
好吧,我用Google搜索并找到了jooq库.我不确定,但看起来它可能适用于我的测试用例.总而言之,我有几张包含大量数据的表格.我想流式传输我的结果集并将此流传输到另一个方法.像这样的东西:
// why return Stream<String>? Because my result set has String type
private Stream<Record> writeTableToStream(DataSource dataSource, String table) {
Stream<Record> record = null;
try (Connection connection = dataSource.getConnection()) {
String sql = "select * from " + table;
try (PreparedStatement pSt = connection.prepareStatement(sql)) {
connection.setAutoCommit(false);
pSt.setFetchSize(5000);
ResultSet resultSet = pSt.executeQuery();
//
record = DSL.using(connection)
.fetch(resultSet).stream();
}
} catch (SQLException sqlEx) {
logger.error(sqlEx);
}
return record;
}
Run Code Online (Sandbox Code Playgroud)
可以请有人建议,我是否正确?谢谢.
更新#2
我在 …
我正在尝试理解Java 8流.我有两节课:
public class UserMeal {
protected final LocalDateTime dateTime;
protected final String description;
protected final int calories;
public UserMeal(LocalDateTime dateTime, String description, int calories) {
this.dateTime = dateTime;
this.description = description;
this.calories = calories;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public String getDescription() {
return description;
}
public int getCalories() {
return calories;
}
}
Run Code Online (Sandbox Code Playgroud)
和:
public class UserMealWithExceed {
protected final LocalDateTime dateTime;
protected final String description;
protected final int calories;
protected final boolean exceed;
public …Run Code Online (Sandbox Code Playgroud) 我正在阅读Java文档并遇到这句话:
除了转义舱口的操作
iterator()和spliterator(),被调用的终端操作时开始执行,并且终端操作完成时结束.
我不确定"逃生舱操作"是什么意思.有人可以解释一下这个词吗?
我有一个这样的课:
class MultiDataPoint {
private DateTime timestamp;
private Map<String, Number> keyToData;
}
Run Code Online (Sandbox Code Playgroud)
我想为每个MultiDataPoint生成
class DataSet {
public String key;
List<DataPoint> dataPoints;
}
class DataPoint{
DateTime timeStamp;
Number data;
}
Run Code Online (Sandbox Code Playgroud)
当然,在多个MultiDataPoints中,"key"可以是相同的.
所以给定一个List<MultiDataPoint>,我如何使用Java 8流转换为List<DataSet>?
这就是我目前在没有流的情况下进行转换的方式:
Collection<DataSet> convertMultiDataPointToDataSet(List<MultiDataPoint> multiDataPoints)
{
Map<String, DataSet> setMap = new HashMap<>();
multiDataPoints.forEach(pt -> {
Map<String, Number> data = pt.getData();
data.entrySet().forEach(e -> {
String seriesKey = e.getKey();
DataSet dataSet = setMap.get(seriesKey);
if (dataSet == null)
{
dataSet = new DataSet(seriesKey);
setMap.put(seriesKey, dataSet);
}
dataSet.dataPoints.add(new DataPoint(pt.getTimestamp(), …Run Code Online (Sandbox Code Playgroud) List<MyObject> myList = new ArrayList<>();
//populate myList here
List<String> nameList = myList.stream()
.map(MyObject::getName)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我可以期望MyObject名称nameList的顺序总是与顺序相同myList吗?
我想要做的是在下面的2个流调用中显示.我想根据某些条件将一个集合拆分为两个新集合.理想情况下,我想在1中进行.我已经看到了用于.map函数的条件,但找不到forEach的任何内容.实现我想要的最好方法是什么?
animalMap.entrySet().stream()
.filter(pair-> pair.getValue() != null)
.forEach(pair-> myMap.put(pair.getKey(), pair.getValue()));
animalMap.entrySet().stream()
.filter(pair-> pair.getValue() == null)
.forEach(pair-> myList.add(pair.getKey()));
Run Code Online (Sandbox Code Playgroud) java ×10
java-stream ×10
java-8 ×8
lambda ×2
closures ×1
jdbc ×1
jooq ×1
list ×1
optional ×1
performance ×1