TccCAgentRes 中包含 label phone name字段筛选ListString labeList 中label与labelList模糊匹配的
您可以使用正则表达式来实现模糊匹配的筛选。首先,将labelList中的每个元素都转换为正则表达式模式,然后遍历TccCAgentRes列表,使用Pattern类的matcher方法来匹配label字段与模式是否匹配。如果匹配成功,将对应的TccCAgentRes对象添加到结果列表中。
以下是一个示例代码:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
List<String> labelList = new ArrayList<>();
labelList.add("ph");
labelList.add("na");
List<TccCAgentRes> tccCAgentResList = new ArrayList<>();
tccCAgentResList.add(new TccCAgentRes("label1", "phone1", "name1"));
tccCAgentResList.add(new TccCAgentRes("label2", "phone2", "name2"));
tccCAgentResList.add(new TccCAgentRes("label3", "phone3", "name3"));
List<TccCAgentRes> filteredList = new ArrayList<>();
for (TccCAgentRes res : tccCAgentResList) {
for (String label : labelList) {
Pattern pattern = Pattern.compile(label, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(res.getLabel());
if (matcher.find()) {
filteredList.add(res);
break;
}
}
}
// 输出结果
for (TccCAgentRes res : filteredList) {
System.out.println(res);
}
}
}
class TccCAgentRes {
private String label;
private String phone;
private String name;
public TccCAgentRes(String label, String phone, String name) {
this.label = label;
this.phone = phone;
this.name = name;
}
public String getLabel() {
return label;
}
public String getPhone() {
return phone;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "TccCAgentRes{" +
"label='" + label + '\'' +
", phone='" + phone + '\'' +
", name='" + name + '\'' +
'}';
}
}
在上述示例中,我们创建了一个包含label、phone和name字段的TccCAgentRes类。然后,我们定义了一个labelList列表,其中包含了要模糊匹配的label模式。我们还创建了一个tccCAgentResList列表,其中包含了要筛选的TccCAgentRes对象。
然后,我们遍历tccCAgentResList列表,并使用嵌套的循环来依次匹配每个TccCAgentRes对象的label字段与labelList中的模式。我们使用Pattern类的compile方法将label转换为正则表达式模式,并使用matcher方法来进行匹配。如果匹配成功,我们将该TccCAgentRes对象添加到filteredList结果列表中。
最后,我们输出filteredList中的结果。
请注意,在上述示例中,我们使用了Pattern.CASE_INSENSITIVE参数来进行大小写不敏感的匹配。如果您需要进行大小写敏感的匹配,可以将Pattern.CASE_INSENSITIVE参数移除
原文地址: http://www.cveoy.top/t/topic/iDhw 著作权归作者所有。请勿转载和采集!