[英]Find a String from one ArrayList as a Charsequence of another ArrayList String in Java 8
我的Java例程应该从一个ArrayList
找到一个String
,作为另一个ArrayList
String
的char
序列。
如果我从目标ArrayList
的源ArrayList
中搜索String
项,则它可以工作。 如果目标ArrayList
项具有更多的字母数字文字,我尝试通过使用char
序列来查找所需部分。
简短的单词示例:
ArrayList_A项目:“ A0B”,“ C1D”,“ E2F”
ArrayList_B项目:“ A0B”
结果:在ArrayList_A
项目中找到项目B(工程)
ArrayList_A项目:“ A0B / C1D”,“ E2F”
ArrayList_B项目:“ A0B”
结果:在ArrayList_A
Items中找到了Item B(这是我另外想要的)
以下是我的部分工作代码。 任何人都可以修复它,以便我可以从解决方案中学习吗?
import java.util.ArrayList;
public class Checking {
static void checkValues () {
ArrayList<String> arrayListA = new ArrayList();
arrayListA.add("A0B/C1D");
arrayListA.add("E2F");
System.out.println("\narrayListA: " + arrayListA);
int sizeLA = arrayListA.size();
System.out.println("arrayListA Length:" + sizeLA);
ArrayList<String> arrayListB = new ArrayList();
arrayListB.add("A0B");
arrayListB.add("E2F");
System.out.println("\narrayListB: " + arrayListB);
int sizeLB = arrayListB.size();
System.out.println("arrayListB Length:" + sizeLB);
ArrayList<String> arrayListFound = new ArrayList();
//Something must be fixed here...
arrayListB.stream().forEach((searchStr) -> {
System.out.print("\nstr Search Item is: " + searchStr);
if(arrayListA.contains((CharSequence)searchStr)){
System.out.print(" - found!" );
arrayListFound.add((String) searchStr);
}
else {
System.out.print(" - not found");
}
});
System.out.println("\n\narrayListFound Items: " + arrayListFound);
int sizeListFound = arrayListFound.size();
System.out.println("sizeListFound Length :" + sizeListFound);
System.out.println("Expected to be fully found in arrayListFound: " + arrayListB);
}
public static void main(String[] args) {
checkValues();
}
}
首先,您应该使用功能样式编程。 forEach()不允许您超出(...)的标准
精确匹配
ArrayList<String> arrayListFound = arrayListB.stream()
.filter(arrayListA::contains)
.collect(Collectors.toList());
对于子串匹配
ArrayList<String> arrayListFound = arrayListB.stream()
.filter(itemB -> arrayListA.stream()
.matchAny(itemA -> itemA.contains(itemB))
)
.collect(Collectors.toList());
您可以流式处理两个集合,并使用String#contains
代替List#contains
。
这将增加所有相应的String
从物体B
包含在A
以arrayListFound
arrayListB.stream().forEach(b ->
arrayListA.stream().forEach(a -> {
if (a.contains(b)){
arrayListFound.add(b);
}
}
));
if(arrayListA.contains((CharSequence)searchStr)){
System.out.print(" - found!" );
arrayListFound.add((String) searchStr);
}
此代码段说:“如果我的列表中包含此字符串,则找到它”。 但是您的字符串A0B
与A0B/C1D
。 遍历两个列表并检查String
而不是ArrayList
的contains
方法,您将找到所需的内容。 例如:
arrayListB.stream().forEach((searchStr) -> {
System.out.print("\nstr Search Item is: " + searchStr);
arrayListA.stream().forEach((sndStr) -> {
if (sndStr.contains(searchStr) || searchStr.contains(sndStr)) {
System.out.print(" - found!");
arrayListFound.add((String) searchStr);
} else {
System.out.print(" - not found");
}
});
});
只能稍微修改您的实现的替代解决方案如下。
arrayListB.stream().forEach((searchStr) -> {
if(arrayListA.stream().anyMatch(str -> str.contains((CharSequence)searchStr))){
System.out.print(" - found!" );
arrayListFound.add((String) searchStr);
}
else {
System.out.print(" - not found");
}
});
一世。 我们可以使用性能优于foreach
anyMatch
。
ii。 在这里,我们正在检查arrayListA
中是否存在任何字符串,该字符串会将arrayListB中的arrayListB
作为子字符串。
要以纯功能性的方式执行此操作而不arrayListFound
列表,可以采用以下方法:
Predicate<String> isInArrayA = searchStr-> arrayListA.stream().anyMatch(str -> str.contains(searchStr));
arrayListFound = (ArrayList<String>) arrayListB.stream()
.filter(isInArrayA)
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.