我有以下 JPA 方法来获取对象流
@Cacheable("accounts")
Stream<Account> findAccounts(int branchCode, int accountCode);
Run Code Online (Sandbox Code Playgroud)
由于流将在调用该方法后关闭,因此当我第二次调用该方法时出现以下错误。
java.lang.IllegalStateException: stream has already been operated upon or closed
Run Code Online (Sandbox Code Playgroud)
我喜欢做的是;缓存流的内容,即帐户并从缓存中读取所有后续调用。实现这一目标的最佳方法是什么?
更新:请注意,我知道使用 List 但需要保留返回类型 Stream。
假设我有这么一段代码:
Map<Consumer, Item> map = myStream.parallel()
.filter(Objects::nonNull)
.collect(Collectors.toConcurrentMap(e->e,
e->e, mergeFunction));
Run Code Online (Sandbox Code Playgroud)
我想做的是在收集完成后对流的每个对象调用一个方法。
例如,
item.setDate(item.getOneDate());
Run Code Online (Sandbox Code Playgroud)
在代码按顺序循环遍历项目之前,放入地图,并在最后调用一些类似上面的代码,设置“日期”。
while(iterator.hasNext()) {
Item blah = iterator.next();
....
// code for putting into map
...
blah.setDate(blah.getOneDate());
}
Run Code Online (Sandbox Code Playgroud)
不确定如何使用 Java 8 流执行此操作。forEach? peek?
我有两个具有以下数据类型的地图,
Map<Pair<Long,String>, List<String>> stringValues;
Map<Pair<Long,String>, List<Boolean>> booleanValues ;
Run Code Online (Sandbox Code Playgroud)
我想将上述地图合并到以下数据结构
Map<Pair<Long,String>, Pair<List<String>,List<Boolean>>> stringBoolValues;
Run Code Online (Sandbox Code Playgroud)
我的输入有两个具有相同键但不同值的映射。我想将它们组合成一对。我可以使用 java 流来实现这一点吗?
我有这个对象:
public class MatchEvent implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Instant dateReceived;
public Instant getDateReceived() {
return dateReceived;
}
public void setDateReceived(Instant dateReceived) {
this.dateReceived = dateReceived;
}
}
Run Code Online (Sandbox Code Playgroud)
我想按收到的日期订购;
matchService
.findAllByDay(today)
.sorted(Comparator.comparing(MatchEvent::dateReceived))
Run Code Online (Sandbox Code Playgroud)
但似乎这是不可能的,因为我遇到了编译错误:
Multiple markers at this line
- The method comparing(Function<? super T,? extends U>) in the type Comparator is not applicable for the arguments
(MatchEvent::dateReceived)
- The type MatchEvent does not define …Run Code Online (Sandbox Code Playgroud) 我在做简单的测试。
@Test
public void whenFilterEmployees_thenGetFilteredEmployees(){
Integer[] empIds = {1,2,3};
List<Optional<Employee>> employees = Stream.of(empIds)
.map(employeeRepository::findById)
.collect(Collectors.toList());
List<Employee> employeeList = employees
.stream().filter(Optional::isPresent)
.map(Optional::get)
.filter(e->e !=null)
.filter(e->e.getSalary()>200000)
.collect(Collectors.toList());
assertEquals(Arrays.asList(arrayOfEmps[2]), employeeList);
}
Run Code Online (Sandbox Code Playgroud)
我的员工表包含数据:
1 Jeff Bezos 100000
2 Bill Gates 200000
3 Mark Zuckerberg 300000
Run Code Online (Sandbox Code Playgroud)
当前测试成功运行。
如您所见,我准备了两个员工列表,即员工和员工列表
我这样做是因为findById方法返回 Optional。我如何使用流 api 以便我可以简单地获取员工列表
List<Employee> employeeList= ....
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据他们的名称获取产品评论地图,并且需要像这样的地图
Map<String, Set<String>> productReviewMap;
Run Code Online (Sandbox Code Playgroud)
但是当我尝试获取地图时,我得到了
Map<String, Set<Set<String>>> productReviewMap
Run Code Online (Sandbox Code Playgroud)
基于以下代码:
Map<String, Set<Set<String>>> productReviewMap = products.stream().collect(
Collectors.groupingBy(Product::getName, Collectors.mapping(Product::getReviews, Collectors.toSet())));
Run Code Online (Sandbox Code Playgroud)
我如何获得预期的地图。我们在流中使用 flatMap 但如何在收集器中实现它?
我有简单的 DataStructure
public class DataStructure {
private String key;
private String value;
//get, set
}
Run Code Online (Sandbox Code Playgroud)
我需要根据键从`List'返回值,我想用流来做Java8的方式。我认为代码不言自明:
public class Main {
public static void main(String args[]) {
List<DataStructure> dataList = new ArrayList<>();
dataList.add(new DataStructure("first", "123"));
dataList.add(new DataStructure("second", "456"));
System.out.println(findValueOldSchool(dataList, "third")); //works ok
System.out.println(findValueStream(dataList, "third")); //throws NoSuchElementException
}
static String findValueOldSchool(List<DataStructure> list, String key) {
for (DataStructure ds : list) {
if (key.equals(ds.getKey())) {
return ds.getValue();
}
}
return null;
}
static String findValueStream(List<DataStructure> list, String key) {
return list.stream()
.filter(ds -> …Run Code Online (Sandbox Code Playgroud) 我目前开始使用 micronaut 和 kotlin。我有 JPA 查询,结果大约有 100 万个。这些结果我想从这个 micronaut 服务流到另一个。
我的查询返回 aallQuery.resultStream类型java.util.stream。
发送服务的控制器:
@Get("/test{value1,value2,value3}")
fun getTestObjects(
value1: String,
value2: String,
value3: String
): Stream<TestObject> {
val entries = testRepository.findAllWhere(value1, value2, value3)
return entries
}
Run Code Online (Sandbox Code Playgroud)
接收服务的客户:
@Get("/data/test{value1,value2,value3}")
override fun getTestObjects(alue1: String,
value2: String,
value3: String) : Stream<TestObject>
Run Code Online (Sandbox Code Playgroud)
JPA 查询如下所示:
val cb = entityManager.criteriaBuilder
val cq = cb.createQuery(TestObject::class.java)
val rootEntry = cq.from(TestObject::class.java)
val predicates = mutableListOf<Predicate>()
predicates.add(cb.like(rootEntry.get<String>("value1"), value1))
predicates.add(cb.equal(rootEntry.get<String>("value2"), value2))
predicates.add(cb.equal(rootEntry.get<Int>("value3"), value3))
val cqAllWhere = cq.select(rootEntry)
.where(cb.or(*predicates.toTypedArray()))
val …Run Code Online (Sandbox Code Playgroud) 所以,我有Stream一些对象,我需要从Stream. 例如从第 2 到第 4 个元素。
Stream<String> strs = Stream.of("qwe", "asd", "zxc", "rty", "fgh", "vbn");
Run Code Online (Sandbox Code Playgroud)
并且仅从第 2 到第 4 个元素返回: asd, zxc, rty .
是否可以?
谢谢!
我有一个里面有 PriorityQueue 字段的类:
public class MyClass<T>{
Queue<T> queue = new PriorityQueue<>();
Run Code Online (Sandbox Code Playgroud)
我想以某种方式从 MyClass 获取一个流并使用 foreach 并希望序列按照我的 PriorityQueue 的优先级顺序运行。最简单的方法是覆盖 stream() 方法:
@Override
public Stream stream() {
return queue.stream();
}
Run Code Online (Sandbox Code Playgroud)
但这不会按优先级顺序公开队列元素。所以问题是:如何让 foreach 流方法表现得像:
while(!queue.isEmpty())
queue.poll();
Run Code Online (Sandbox Code Playgroud) java-stream ×10
java ×7
java-8 ×4
collections ×1
comparator ×1
icomparable ×1
lambda ×1
micronaut ×1
pagination ×1
rest ×1
rx-java2 ×1
spring ×1
spring-boot ×1
stream ×1