我有两种方法,它们的条件不同。那么如何将两种方法合而为一呢?
private void filterById(String keyW) {
neModel.setRowCount(0);
for (int i = neList.size() - 1; i > -1; i--) {
//CONDITION:
if (String.valueOf(neList.get(i).getId()).toLowerCase().contains(keyW.toLowerCase())) {
Object[] aRow = createARow(i);
neModel.addRow(aRow);
}
}
}
private void filterByTitle(String keyW) {
neModel.setRowCount(0);
for (int i = neList.size() - 1; i > -1; i--) {
//CONDITION:
if (neList.get(i).getTitle().toLowerCase().contains(keyW.toLowerCase())) {
Object[] aRow = createARow(i);
neModel.addRow(aRow);
}
}
}
*'neList'是'News'的ArrayList 。新闻类具有属性:ID,标题,文本等。
答案 0 :(得分:2)
使用Java 8+,您可以尝试使用功能接口并将差异作为策略。
类似这样的东西(因为我没有编译器的atm,所以没经过试验):
QUEUE_DRIVER=sync
或者在private void filter(String keyW, Function<News, String> propertyExtractor) {
neModel.setRowCount(0);
//note that this could probably be replaced by for(News news : newList)
//but in that case you'd either have to change createARow to not need the index or increment the index in the loop body
for (int i = neList.size() - 1; i > -1; i--) {
if (propertyExtractor.apply(neList.get(i)).toLowerCase().contains(keyW.toLowerCase())) {
Object[] aRow = createARow(i);
neModel.addRow(aRow);
}
}
}
//Examples of how to use it, you don't have to keep those (delegating) methods
private void filterById(String keyW) {
filter( keyW, e -> String.valueOf(e.getId()));
}
private void filterByTitle(String keyW) {
filter( keyW, News::getTitle);
//or: filter( keyW, e -> e.getTitle());
}
中传递Function<T, S>
而不是Predicate<T>
,它提供了整个条件,只需执行if(predicate.test(neList.get(i))) { ... }
。
编辑:根据OP的评论,将T
替换为News
答案 1 :(得分:2)
如果还不能使用Java 8功能,则只需定义一个enum
:
enum Filter {
ById,
ByTitle
}
然后将其作为标志传递:
private void filter(Filter how, String keyW) {
neModel.setRowCount(0);
for (int i = neList.size() - 1; i > -1; i--) {
String value;
switch (how) {
case ById:
value = String.valueOf(neList.get(i).getId());
break;
// ... add more cases here ...
default:
value = neList.get(i).getTitle();
break;
}
//CONDITION:
if (value.toLowerCase().contains(keyW.toLowerCase())) {
Object[] aRow = createARow(i);
neModel.addRow(aRow);
}
}
}
示例调用:
x.filter(Filter.ById, "");
x.filter(Filter.ByTitle, "");
答案 2 :(得分:0)
如果您不想使用功能参数,则可以只创建1个方法,并使用一个与所需过程相对应的额外参数。
此额外参数可以是枚举,或者如果只有两种情况1 布尔标志:
private void filterBy(boolean byId, String keyW) {
neModel.setRowCount(0);
for (int i = neList.size() - 1; i > -1; i--) {
String what = byId ? String.valueOf(neList.get(i).getId()) : neList.get(i).getTitle();
if (what.toLowerCase().contains(keyW.toLowerCase())) {
Object[] aRow = createARow(i);
neModel.addRow(aRow);
}
}
}
现在这样称呼:
filterBy(true, yourkey) // instead of filterById(yourkey)
或
filterBy(false, yourkey) // instead of filterByTitle(yourkey)
答案 3 :(得分:0)
public void filterBy(String keyW, Function<Data, String> function) {
String key = keyW.toLowerCase();
neModel.setRowCount(0);
IntStream.range(0, neList.size())
.mapToObj(i -> function.apply(neList.get(neList.size() - i - 1)).toLowerCase().contains(key) ? createARow(i) : null)
.filter(Objects::nonNull)
.forEach(row -> neModel.addRow(row));
}
答案 4 :(得分:-2)
您可以输入||条件相同的条件之间的(OR)运算符。
如果索引冲突,则需要再管理一个索引变量。