I am novice in Java, but I would like to use Parallel Streams instead For statement. So I have an ArrayList of pojo object like bellow and try check field of one object with second object:
List<DataOne> dol = new ArrayList<>();
/// dol.add(new DataOne("fieldA", "fieldB", "fieldC")); ...
List<DataTwo> dtl = new ArrayList<>();
/// dol.add(new DataTwo("field1", "field2", "field3")); ...
boolean cc = dol.parallelStream()
.filter(Objects::nonNull)
.filter(a -> a.getFieldA() != null && a.getFieldB() != null && a.getFieldC() != null)
.anyMatch(a ->
dtl.parallelStream()
.filter(Objects::notNull)
.filter(b -> b.getField1 != null && b.getField2 != null && b.getField3 != null)
.anyMatch(b -> b.getField1.contains(a.getFieldA)));
But it is not working... What I did wrong?
You are using contains() method to find the matching fields. DataTwo.getField1().contains(DataOne.getFieldA()) ---> "field1".contains("fieldA") --> returns "false" since "field1" does not contain any "fieldA". But if DataTwo.field1 was something like "fieldAekstra" then DataTwo.getField1().contains(DataOne.getFieldA()) --> would return true because "fieldAekstra" contains one "fieldA".
Example below is running correctly and outputs "Match Found". I just corrected some typos. Overall your code is working, it is just that there were no matching strings in your example list items.
List<DataOne> dol = new ArrayList<>();
dol.add(new DataOne("fieldA", "fieldB", "fieldC"));
dol.add(new DataOne("fieldD", "fieldF", "fieldH"));
dol.add(new DataOne("fieldK", "fieldM", "fieldN"));
List<DataTwo> dtl = new ArrayList<>();
dtl.add(new DataTwo("fieldAEkstra", "fieldBEkstra", "fieldCEkstra"));
dtl.add(new DataTwo("fieldDEkstra", "fieldFEkstra", "fieldHEkstra"));
dtl.add(new DataTwo("fieldTEkstra", "fieldOEkstra", "fieldWEkstra"));
/*dtl.add(new DataTwo("field1", "field2", "field3"));
dtl.add(new DataTwo("field4", "field5", "field6"));
dtl.add(new DataTwo("field7", "field8", "field9")); */
boolean cc = dol.parallelStream()
.filter(Objects::nonNull)
.filter(a -> a.getFieldA() != null && a.getFieldB() != null && a.getFieldC() != null)
.anyMatch(a ->
dtl.parallelStream()
.filter(Objects::nonNull)
.filter(b -> b.getField1() != null && b.getField2() != null && b.getField3() != null)
.anyMatch(b -> b.getField1().contains(a.getFieldA())));
if(cc)
System.out.println("Match found!");
else
System.out.println("No Match !");
Output:
Match found!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.