如何从循环中删除 if else 条件?
How to remove if else condition from loop?
我有一个类似于下面的代码片段,
public ArrayList getReport(reportJDOList,accountType)
{
String abc = "";
for(ReportJDO reportJDO : reportJDOList)
{
if(accountType.equals("something")
abc = reportJDO.getThis();
else
abc = reportJDO.getThat();
//somecode goes here
}
returning List;
}
正如我在迭代之前知道 accountType 的值,我不希望对列表中的每个条目进行此检查,因为如果实例的 reportJDOList 的大小为 10000,它会导致大量检查。我们如何避免这件事发生?提前致谢:)
如果你想保存字符串比较,在循环之前进行一次并将结果存储在布尔变量中:
String abc = "";
boolean isThis = accountType.equals("something");
for(ReportJDO reportJDO : reportJDOList) {
abc = isThis ? reportJDO.getThis() : reportJDO.getThat();
//somecode goes here
}
您确实可以执行一次检查并实现 2 个循环:
if(accountType.equals("something") {
for(ReportJDO reportJDO : reportJDOList) {
abc = reportJDO.getThis();
}
} else {
for(ReportJDO reportJDO : reportJDOList) {
abc = reportJDO.getThat();
}
}
显然,您可以通过以下任一方法改进您的设计
- 将循环分成两种不同的方法
- 使用命令模式,即在不同的命令中实现循环体并执行循环。
- 使用Guava的函数(只是对#2的改进)
- 使用 java 8 个流。
我会投票支持干净的编码——执行一次检查并将逻辑委托给私有方法,每个方法单独执行循环。这会重复循环的代码,但如果在某些时候您需要在 SomethingReport 中做一些没有在 OtherReport 中重复的事情,它会提供最大的灵活性。
public ArrayList getReport(reportJDOList,accountType) {
if("soemthing".equals(accountType)) {
return getSomethingReport(reportJDOList);
} else {
return getOtherReport(reportJDOList);
}
}
private ArrayList getSomethingReport(reportJDOList) {
[...]
}
interface AccountHandler {
String get(Report r);
}
AccountHandler thisHandler= new AccountHandler() {
@Override
public String get(Report r) {
return r.getThis();
}
};
AccountHandler thatHandler= new AccountHandler() {
@Override
public String get(Report r) {
return r.getThat();
}
};
//...............
AccountHandler ah;
ah = (what.equalsIgnoreCase("this")) ? thisHandler : thatHandler;
Report r=new Report();
// loop
ah.get(r);
//Using reflection:
Report r = new Report();
Method thisMethod = r.getClass().getDeclaredMethod("getThis");
Method thatMethod = r.getClass().getDeclaredMethod("getThat");
Method m = (what.equalsIgnoreCase("this")) ? thisMethod : thatMethod;
m.invoke(r);
我有一个类似于下面的代码片段,
public ArrayList getReport(reportJDOList,accountType)
{
String abc = "";
for(ReportJDO reportJDO : reportJDOList)
{
if(accountType.equals("something")
abc = reportJDO.getThis();
else
abc = reportJDO.getThat();
//somecode goes here
}
returning List;
}
正如我在迭代之前知道 accountType 的值,我不希望对列表中的每个条目进行此检查,因为如果实例的 reportJDOList 的大小为 10000,它会导致大量检查。我们如何避免这件事发生?提前致谢:)
如果你想保存字符串比较,在循环之前进行一次并将结果存储在布尔变量中:
String abc = "";
boolean isThis = accountType.equals("something");
for(ReportJDO reportJDO : reportJDOList) {
abc = isThis ? reportJDO.getThis() : reportJDO.getThat();
//somecode goes here
}
您确实可以执行一次检查并实现 2 个循环:
if(accountType.equals("something") {
for(ReportJDO reportJDO : reportJDOList) {
abc = reportJDO.getThis();
}
} else {
for(ReportJDO reportJDO : reportJDOList) {
abc = reportJDO.getThat();
}
}
显然,您可以通过以下任一方法改进您的设计
- 将循环分成两种不同的方法
- 使用命令模式,即在不同的命令中实现循环体并执行循环。
- 使用Guava的函数(只是对#2的改进)
- 使用 java 8 个流。
我会投票支持干净的编码——执行一次检查并将逻辑委托给私有方法,每个方法单独执行循环。这会重复循环的代码,但如果在某些时候您需要在 SomethingReport 中做一些没有在 OtherReport 中重复的事情,它会提供最大的灵活性。
public ArrayList getReport(reportJDOList,accountType) {
if("soemthing".equals(accountType)) {
return getSomethingReport(reportJDOList);
} else {
return getOtherReport(reportJDOList);
}
}
private ArrayList getSomethingReport(reportJDOList) {
[...]
}
interface AccountHandler {
String get(Report r);
}
AccountHandler thisHandler= new AccountHandler() {
@Override
public String get(Report r) {
return r.getThis();
}
};
AccountHandler thatHandler= new AccountHandler() {
@Override
public String get(Report r) {
return r.getThat();
}
};
//...............
AccountHandler ah;
ah = (what.equalsIgnoreCase("this")) ? thisHandler : thatHandler;
Report r=new Report();
// loop
ah.get(r);
//Using reflection:
Report r = new Report();
Method thisMethod = r.getClass().getDeclaredMethod("getThis");
Method thatMethod = r.getClass().getDeclaredMethod("getThat");
Method m = (what.equalsIgnoreCase("this")) ? thisMethod : thatMethod;
m.invoke(r);