如何使用java区分下面的结果?我的代码如下:
Page<Object[]> datasetresults=repository.findtest(tid,
PageRequest.of(page, per_page, Sort.by(s, sort)));
for (Object[] object : datasetresults.getContent()) {
if (object[0] instanceof onetable ) {
onetable onetable = (onetable) object[0];
TestBean test = new TestBean();
}
}
我需要使用 tid
进行区分以获得一个唯一的结果集。
最佳答案
要根据“tid”列区分结果并获得唯一的结果集,您可以在迭代数据集结果时使用 Set 来跟踪唯一的 tid 值。您可以通过以下方式修改代码来实现此目的:
Page<Object[]> datasetresults = repository.findtest(tid, PageRequest.of(page, per_page, Sort.by(s, sort)));
// Create a Set to keep track of unique tid values
Set<Long> uniqueTids = new HashSet<>();
for (Object[] object : datasetresults.getContent()) {
if (object[0] instanceof onetable && object[1] instanceof twotable) {
onetable onetable = (onetable) object[0];
twotable twotable = (twotable) object[1];
// Check if the tid is unique
if (uniqueTids.add(onetable.getTid())) {
// This tid is unique, process the result
TestBean test = new TestBean();
// Add your code here to process the unique result
}
}
}
在此代码中,我们使用名为 uniqueTids 的 Set 来跟踪唯一的“tid”值。在处理每个结果之前,我们使用 uniqueTids.add(onetable.getTid()) 检查“tid”是否已在集合中。如果是新的“tid”,我们就会处理结果。这可确保仅处理唯一的“tid”值,从而为您提供基于“tid”的不同结果集。
关于java - 如何使用java区分下面的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77155291/