java - 使用 next() 为可迭代查找下一个唯一元素
问题描述
我需要next()
在我的包装类中重写迭代器的函数,这样每次我调用时next()
,它都会遍历ScrollableResults
,添加细节,并最终返回唯一的记录。
由于事情已经就位,除了详细名称和详细值之外,具有相同序列号的每条记录都应该完全相同。我需要将这些详细信息添加到同一记录中(就是这样record.addDetail(name, value)
做的)并最终返回该唯一记录。以下是我的自定义需要发生的事情的细分next()
:
遍历
scrollableResults
,将详细名称和详细值添加到唯一记录(它可以添加到唯一记录)。一旦光标遇到不同的序列号,它应该跳出循环并返回唯一的记录/对象。
- 一旦返回,下一次调用
next()
将重新开始该过程,除了光标会从它停止的地方开始,previousSequence
也应该从它离开的地方开始。
包装器.java:
public class Wrapper<T> implements Iterable<T>, AutoCloseable {
private ScrollableResults scrollableResults;
private Session session;
public Wrapper(ScrollableResults scrollableResults, Session session) {
this.scrollableResults = scrollableResults;
this.session = session;
}
public Iterator<T> iterator() { return new WrapperIterator(); }
@Override
public void close() {
scrollableResults.close();
}
public class WrapperIterator implements Iterator<T> {
private Integer previousSequence;
@Override
public boolean hasNext() {
boolean hasNext = scrollableResults.next();
return hasNext;
}
/**
* This next() method should return the next UNIQUE record.
* But before returning the unique record, it should add all the appropriate details to the respective record.
*/
@Override
public T next() {
boolean hasNext = scrollableResults.next();
if (!hasNext) {
throw new NoSuchElementException();
}
// For previousSequence, first result needs to be retrieved explicitly
Object[] row = (Object[]) scrollableResults.get();
Record record = (Record) row[0];
Integer currentSequence = previousSequence = record.getSequence();
String detailName = (String) row[1];
String detailValue = (String) row[2];
record = setDetails(record, detailName, detailValue);
Object[] mainRow = row;
// First one should always be unique since it's starting a new set of records of same sequence.
while(scrollableResults.next()) {
// Get record
row = (Object[]) scrollableResults.get();
record = (Record) row[0];
// Update current sequence
currentSequence = record.getSequence();
// If current sequence is different from previous sequence, break
// First result needs to be accounted for
if(!currentSequence.equals(previousSequence)) {
previousSequence = currentSequence;
session.evict(row);
return (T) row;
}
// Add detail
detailName = (String) row[1];
detailValue = (String) row[2];
record = setDetails(record, detailName, detailValue);
}
previousSequence = currentSequence;
return (T) mainRow;
}
private Record setDetails(Record record, String detailName, String detailValue) {
record.addDetail(detailName, detailValue);
return record;
}
}
}
我一直在尝试实现这一点,这就是我所得到的。我目前的方法可以遍历并详细说明,但我的问题是返回正确的行。由于当前序列需要与前一个序列进行检查,因此我无法返回新序列记录之前的最后一条记录,因为光标已经过了它。
关于如何解决这个问题的任何建议?任何事情都将不胜感激!
注意:迭代器不能向后遍历。由于滚动模式需要为 FORWARD_ONLY 的 PSQL 错误,滚动模式已设置为 FORWARD_ONLY。
解决方案
推荐阅读
- asp.net-core - 从 Azure 中的 MVC Core 调用时保护 WEB API Core
- python - 可以用 Python 编写的最简单的客户端/服务器对是什么?
- reactjs - react 中 webpack build 和 npm run build 的区别
- php - 注意:升级到 php 7.4 后尝试访问 bool 类型值的数组偏移量
- angular - 如何从 Angular 向 Laravel 发送 GET 请求(用于登录)
- django - Django过滤器获取外键模型列表
- r - 在 DT 表中将行拆分为 2
- python - 如何将与索引并行创建的多个图像通过管道传输到 ffmpeg,以便它可以匹配图像创建的速度?
- java - 我可以在 switch case 中匹配表达式的不同部分吗?
- varnish - 基于 Cookie 的清漆响应