我有一个流并行执行一系列操作,然后我必须将结果写入文件,所以我需要写入操作是顺序的,但是它需要作为一个流来执行,我有很多数据要写入,我不能使用中间集合。有没有办法做到这一点?
我想到了一个看起来不太干净的解决方案,那就是让写法同步。这种方法是唯一可能的吗?还有其他方法吗?
谢谢你。
我对 Java 8 中的流很陌生,所以我的方法可能是错误的。
我有 2 个对象如下
object1 {
BigDecimal amount;
Code1 code1;
Code2 code2;
Code3 code3;
String desc;
}
object2 {
BigDecimal amount;
Code1 code1;
Code2 code2;
Code3 code3;
}
Run Code Online (Sandbox Code Playgroud)
所以我想收集 code1 && code2 && code3 相同的所有 object1,然后将数量相加将其添加到 object2 列表中。
我没有代码来做到这一点......我想编写一个代码来完成这项工作我正在尝试从http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html实现一些东西
或者按部门计算所有工资的总和:
// Compute sum of salaries by department
Map<Department, Integer> totalByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
Run Code Online (Sandbox Code Playgroud) 好的,所以我有一个要过滤的地图列表,然后收集到另一个地图列表。每次我在整个过程中放入该列表时,我都会得到一个对象列表...帮助?!
List<Map<String, Object>> segments = (List<Map<String, Object>>) (List<?>) query.getResultList();
List<Map<String, Object>> segmentsWithMoreVersions = segments.stream()
.filter((Object s) -> {
Object[] ss = (Object[]) s;
if(((Long) ss[2]) == 1)
{
return false;
}
return true;
})
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如您所见...在过滤器方法中,我使用了一个对象,因为我无法将 s 变量设为 Map。当我尝试时,我得到一个 ClassCastException。
编辑:好的,也许需要更多信息......所以我的segments变量是这样的:
[
{'name': 'ABC', 'version': 1, 'ct': 1},
{'name': 'AAA', 'version': 1, 'ct': 1},
{'name': 'BFD', 'version': 1, 'ct': 4},
{'name': 'SDE', 'version': 1, 'ct': 1}
]
Run Code Online (Sandbox Code Playgroud)
我想要做的是过滤掉所有 ct 为 1 的地图。过滤器方法正在查看该对象(因为我无法将其转换为地图)并检查它是否 == 为 1 ,如果是这样,它不会将其返回到流中。
Edit2:评论后,我编辑为以下内容:
List<Map<String, …Run Code Online (Sandbox Code Playgroud) 我正在使用 Java 8 Stream,在其中迭代两个集合,在通过过滤器后,我想将流中的 bigdecimal 变量之一与外部 bigDecimal 变量“restrictionsNumber”相加
这是我的代码:
final BigDecimal restrictionsNumber = cmd.amount.getNumberOfUnits();
order.products()
.stream()
.flatMap(product -> product.getRestrictions()
.stream()
.filter(restriction -> restriction.equals(newProductRestriction))
.map(restriction -> restrictionsNumber.add(product.getAmount()
.getNumberOfUnits())));
Run Code Online (Sandbox Code Playgroud)
最后一张地图是我试图将两个 bigdecimals 相加的地图。我知道我做错了什么。谁能给我一个关于如何使用 Stream.js 做这件事的建议。
我正在尝试重构这个旧的时尚代码
final BigDecimal restrictionsNumber = cmd.amount.getNumberOfUnits();
for (Product product : order.products()) {
for (String oldProductRestriction : product.getRestrictions()) {
if (oldProductRestriction.equals(newProductRestriction)) {
restrictionsNumber = restrictionsNumber.add(product.getAmount()
.getNumberOfUnits());
}
}
}
Run Code Online (Sandbox Code Playgroud)
问候。
假设我有员工数据列表。
class Employee {
private long id;
private String name;
private int age;
// Constrctor
// setters & getters
}
List<Employee> empList = ...;
Run Code Online (Sandbox Code Playgroud)
使用上面的empList,如何使用Java8流将年龄> 20的两个不同列表分成一个列表,将年龄<20的两个列表分成一个列表。
我试图在Integer流上使用reduce方法来获取所有值的总和。但出现语法错误,无法找出错误。
List<Integer> ee = new ArrayList<Integer>();
Function<? super Integer, ? extends Integer> f3 = x -> x / 2;
BinaryOperator<? extends Integer> accumulator = (x, y) -> x + y;
ee.stream().map(f3).reduce(new Integer(0), accumulator);
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
The method reduce(capture#2-of ? extends Integer, BinaryOperator<capture#2-of ? extends Integer>) in the type Stream<capture#2-of ? extends Integer> is not applicable for the arguments (Integer, BinaryOperator<capture#7-of ? extends Integer>)
Run Code Online (Sandbox Code Playgroud) 我有简单对象的列表:
private String unit;
private Double value;
Run Code Online (Sandbox Code Playgroud)
列表看起来像这样:
f, 1.0;
ml, 15.0;
g, 9.0
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的函数,希望将这些值分组并以unit为键将它们放入地图,并将对象列表作为值,但是我想像在原始列表中那样保存顺序。这是我目前的解决方案:
myList.stream()
.collect(groupingBy(MyObject::getUnit));
Run Code Online (Sandbox Code Playgroud)
但是之后,我的地图按字母顺序排序:f,g,ml,而不是f,ml,g。有没有其他分组方法可以解决?
我正在尝试将玩家列表转换为地图。播放器包含名称并作为变量运行。
List<Player> runnerList = Arrays.asList(new Player("Virat", 4654), new Player("Jaddu", 5798),
new Player("Dhoni", 4581), new Player("Virat", 8709), new Player("Dhoni", 4711),
new Player("Virat", 4541));
Run Code Online (Sandbox Code Playgroud)
我的问题是我试图通过使用流合并运行来转换为地图,而无法通过。
尝试每个并合并值,如下所示,并获得预期结果。
playerList.forEach(n -> {
mapVal.merge((n.getName()), (n.getDistance()), (val1, val2) -> IntStream.of(val1, val2).sum());
});
Run Code Online (Sandbox Code Playgroud)
寻找使用流的解决方案的结果将是{Dhoni = 9292,Jaddu = 5798,Virat = 17904}。
我确实有一些问题,无法理解当将Stream来自Spring数据jpa的查询结果接收到的手动包装到a中时,幕后是怎么回事Flux。
考虑以下:
实体:
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class TestEntity {
@Id
private Integer a;
private Integer b;
}
Run Code Online (Sandbox Code Playgroud)
仓库:
public interface TestEntityRepository extends JpaRepository<TestEntity, Integer> {
Stream<TestEntity> findByBBetween(int b1, int b2);
}
Run Code Online (Sandbox Code Playgroud)
简单的测试代码:
@Test
@SneakyThrows
@Transactional
public void dbStreamToFluxTest() {
testEntityRepository.save(new TestEntity(2, 6));
testEntityRepository.save(new TestEntity(3, 8));
testEntityRepository.save(new TestEntity(4, 10));
testEntityFlux(testEntityStream()).subscribe(System.out::println);
testEntityFlux().subscribe(System.out::println);
Thread.sleep(200);
}
private Flux<TestEntity> testEntityFlux() {
return fromStream(this::testEntityStream);
}
private Flux<TestEntity> testEntityFlux(Stream<TestEntity> testEntityStream) {
return fromStream(() -> testEntityStream);
}
private Stream<TestEntity> testEntityStream() {
return …Run Code Online (Sandbox Code Playgroud) java transactions spring-data-jpa java-stream project-reactor
作为学习Java 8+ Streams的练习,我想将一些简单的Codility实现转换为Stream解决方案。
例如,BinaryGap问题..使用Streams的一个简单的线性解决方案可能类似于:
public static int solution(int N) {
return Integer.toBinaryString(N).chars().
filter(x -> x == 1).whichIndexes().diff().max();
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,虽然,whichIndexes并且diff不存在。我需要一种方法来获取已过滤元素的索引,然后计算它们的成对差异,这将是基于Streams的单线解决方案的良好起点。
更新:这是我的C ++ BinaryGap解决方案,但是Java非Stream-ed版本将非常相似:
#include <bitset>
#include <iostream>
#include <math.h>
using namespace std;
int solution(int N) {
bitset<32> bs(N);
int maxGap = 0;
std::size_t i = 0;
while (bs[i] == 0) {
i++;
}
int startPos = i;
for (; i < bs.size(); ++i) {
if (bs[i] == 1) {
int gap = i - startPos …Run Code Online (Sandbox Code Playgroud) java-stream ×10
java ×9
java-8 ×6
collectors ×2
grouping ×2
algorithm ×1
dictionary ×1
filtering ×1
hashmap ×1
lambda ×1
list ×1
transactions ×1