If you just need to test basic equality, this can be done with the basic JDK without modifying the input lists in the one line
!Collections.disjoint(list1, list2);
If you need to test a specific property, that's harder. I would recommend, by default,
list1.stream()
.map(Object1::getProperty)
.anyMatch(
list2.stream()
.map(Object2::getProperty)
.collect(toSet())
::contains)
...which collects the distinct values in list2 and tests each value in list1 for presence.
If you just need to test basic equality, this can be done with the basic JDK without modifying the input lists in the one line
!Collections.disjoint(list1, list2);
If you need to test a specific property, that's harder. I would recommend, by default,
list1.stream()
.map(Object1::getProperty)
.anyMatch(
list2.stream()
.map(Object2::getProperty)
.collect(toSet())
::contains)
...which collects the distinct values in list2 and tests each value in list1 for presence.
To shorten Narendra's logic, you can use this:
boolean var = list1.stream().anyMatch(element -> list2.contains(element));
Or even simpler
list1.stream().anyMatch(list2::contains);
Your code is more complicated than it needs to be, but it gets the job done.
The awkward part of your code is the use of the various index values i, j, and k. Really, you don't need them at all. If you rename things a bit, and use "enhanced-for" loops, it becomes:
public boolean compareTwoList(List<String> models, List<String> titleOfVehicles) {
for(String title : titleOfVehicles) {
boolean found = false;
for(String model : models) {
if(title.contains(model)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
Note that the logic is essentially the same, but you focus on the important things. The found variable is a better name, and it's scope is limited to inside the outer for-loop. The hard-to-understand k loop-terminator is removed.
Note, using streams, and a regular expression, would actually be more compact solution, but may not be as readable... I played with the stream version and a regex, and got:
public static boolean compareTwoList(List<String> models, List<String> titleOfVehicles) {
String pattern = models.stream()
.map(Pattern::quote)
.collect(Collectors.joining("|", ".*(", ").*"));
Pattern re = Pattern.compile(pattern);
return titleOfVehicles.stream()
.allMatch(t -> re.matcher(t).matches());
}
(which you can see working here: https://ideone.com/8sqKu7 )
The current code works but it is very awkward, as rolfl pointed out. I would add that you should never compare booleans with true or false, like what you're doing in:
} else if(k == models.size() - 1 && flag == false) {
return false;
}
Instead, have
} else if (k == models.size() - 1 && !flag) {
return false;
}
You can actually accomplish this task in a single, clear and easy line using the Stream API:
public boolean compareTwoList(List<String> models, List<String> titleOfVehicles) {
return titleOfVehicles.stream().allMatch(t -> models.stream().anyMatch(t::contains));
}
This does exactly what is written: it returns whether all elements in the given titles contains any of the given models.
Both allMatch and anyMatch are short-circuiting operations, so it will behave exactly like your current code.