The Sample Code which needs a solution?
public class TestJJava {
public static void main(String[] args) {
// TODO Auto-generated method stub
You aren't currently comparing the first three characters because subList
doesn't actually apply a substring function (here it copies the list as is). You can also initialize your List
more efficiently (and you should program to the List
interface). I would stream()
it and map
each element using String.substring
and anyMatch
. Like,
String abc = "123XXXXX0";
List lstValues = new ArrayList<>(List.of("111XXXX1", "122XXX1", "123XXXX1"));
if (lstValues.stream().map(x -> x.substring(0, 3)).anyMatch(abc.substring(0, 3)::equals)) {
System.out.println("**** Match Found ***");
} else {
System.out.println("**** No Match Found ****");
}
Which outputs
**** Match Found ***