I am new to Java streams but need to master by practice really!
The collection input is made up of strings e.g. [name][dot][country], example as follows:
- JAMES.BRITAIN
- JOHN.BRITAIN
- LEE.BRITAIN
- GEORGE.FRANCE
- LEON.FRANCE
- MARSELLE.FRANCE
- KOFI.GHANA
- CHARLIE.GHANA
Given a list of countries, How do I return a list of items whose suffixes matches the supplied countries
So, if I supplied a list parameter containing Britain as item , the resulting list should be:
- JAMES.BRITAIN
- JOHN.BRITAIN
- LEE.BRITAIN
In the real code the streams statement below gives me the list to be filtered i.e.:
List<String> allSolrCollections = (List<String>) findAllCollections()
.getJsonArray(SOLR_CLOUD_COLLECTION)
.getList()
.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());
That is:
- JAMES.BRITAIN
- JOHN.BRITAIN
- LEE.BRITAIN
- GEORGE.FRANCE
- LEON.FRANCE
- MARSELLE.FRANCE
- KOFI.GHANA
- CHARLIE.GHANA
But, I had to use the following non-stream code to filter by suffice name:
private List<String> getCollectionsFilteredBySuffices(List<String> listParam, List<String> allItemsList) {
List<String> finalList = new ArrayList<>();
for (String country: listParam) {
for (String item: allItemsList) {
if (item.endsWith(country)) {
finalList .add(item);
}
}
}
return finalList ;
}
Can I do both logic in a single java stream statement e.g.
List<String> allSolrCollections = (List<String>) findAllCollections()
.getJsonArray(SOLR_CLOUD_COLLECTION)
.getList()
.filter(//all items with suffices matching content of list param)
.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());