使用不同对象优化循环Java
我想合并到一个使用不同对象优化循环Java,java,loops,Java,Loops,我想合并到一个for循环中,与不同的对象合并,如下所示: for (BBPostLikeLogs postDetail : bbPostLikeLogs) { if (postDetail.getType() == 4) { NewFaqQuestion questions = newFaqQuestionRepository.findByIdQuestion(postDetail.getId_pos
for
循环中,与不同的对象合并,如下所示:
for (BBPostLikeLogs postDetail : bbPostLikeLogs) {
if (postDetail.getType() == 4) {
NewFaqQuestion questions = newFaqQuestionRepository.findByIdQuestion(postDetail.getId_post());
if (questions == null) continue;
listObjPosts.add(buildObjPostFromQuestions(questions,userAuth));
}else{
BBPost bbPost = bbPostRepository.findById(postDetail.getId_post());
if(bbPost == null) continue;
listObjPosts.add(buildObjPostFromPosts(bbPost,userAuth));
}
}
及
只有一个(用于循环) 您可以使用方法
getType()
和getId()
创建一个接口,并像那样为(您的接口详细信息:bbPostLikeLogs){//do which}
在循环中迭代
public interface YourInterface {
int getId();
int getType();
}
// implement this interface in your classes
public class PostResult implements YourInterface {
// Your class code here
@Override
public int getId() {
return this.get_id();
}
@Override
public int getType() {
return this.getType();
}
}
public class BBPostLikeLogs implements YourInterface {
// Your class code here
@Override
public int getId() {
return this.getId_post();
}
@Override
public int getType() {
return this.getType();
}
}
// and finally the loop
for (YourInterface detail : bbPostLikeLogs) {
if (detail.getType() == 4) {
NewFaqQuestion questions = newFaqQuestionRepository.findByIdQuestion(detail.getId());
if (questions == null) continue;
listObjPosts.add(buildObjPostFromQuestions(questions,userAuth));
} else {
BBPost bbPost = bbPostRepository.findById(detail.getId());
if(bbPost == null) continue;
listObjPosts.add(buildObjPostFromPosts(bbPost,userAuth));
}
}
您可以发布
PostResult
和BBPostLikeLogs
的代码吗?您正在迭代两个不同的iterable对象,除非这两个iterable大小相同,否则您实际上需要两个循环。您可以做的是使用getterget\u id()
和getType()创建一个接口“YourInterfaceName”
并在PostResult
和BBPostLikeLogs
中实现此接口。然后您可以为(YourInterfaceName对象:bbPostLikeLogs){//do which}Hi#lub0v迭代like,谢谢您的评论。java中是否有我们需要的所有对象?就像当我们从数据库获取数据时,我们需要创建新的对象?@BeoStepens Java是一种面向对象的编程语言,所以是的,您几乎总是需要一个对象。当您从数据库中获取数据时,您必须将其存储在内存中的某个位置以生成一些业务逻辑,对吗?对象是最方便的方法。
public interface YourInterface {
int getId();
int getType();
}
// implement this interface in your classes
public class PostResult implements YourInterface {
// Your class code here
@Override
public int getId() {
return this.get_id();
}
@Override
public int getType() {
return this.getType();
}
}
public class BBPostLikeLogs implements YourInterface {
// Your class code here
@Override
public int getId() {
return this.getId_post();
}
@Override
public int getType() {
return this.getType();
}
}
// and finally the loop
for (YourInterface detail : bbPostLikeLogs) {
if (detail.getType() == 4) {
NewFaqQuestion questions = newFaqQuestionRepository.findByIdQuestion(detail.getId());
if (questions == null) continue;
listObjPosts.add(buildObjPostFromQuestions(questions,userAuth));
} else {
BBPost bbPost = bbPostRepository.findById(detail.getId());
if(bbPost == null) continue;
listObjPosts.add(buildObjPostFromPosts(bbPost,userAuth));
}
}