-2

I want to create a query if the getData() return "yes". So added the filter for the same.

Now if the value of getData() is "yes" then I want to concat the three parameter which I am getting from the object.

Now for the list of object I want to join all the value which is coming from

(f -> f.getF()+" "+f.getO()+" "+f.getV())

with " and " as delimiter but not able to figure it out.

String query = data.stream()
    .filter(f -> f.getData().equalsIgnoreCase("yes"))
    .map(f -> f.getF()+" "+f.getO()+" "+f.getV())
    .collect(Collectors.joining(" and","how to do this" , suffix));

for example, we are getting data from that object as

f.getF()--------"column name"

f.getO()--------"="

f.getV()--------"2"

so I want to add all the condition with and delimiter and produce a single String with all the conditions.

I think I can append "and" in the map() and later remove the last "and" from the string

something like this

columnname = 2 and columnnam1 > 1 and column2 = 6

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sparsh610
  • 1,552
  • 3
  • 27
  • 66

2 Answers2

3

If F is a column name, O is an operator and V is a value and if you need to get expression like column1 = 1 and column2 > 2 and ... then you just need to use Collectors.joining(" and "):

String query = data.stream() // stream1 - original stream of the data collection
    .filter(f -> f.getData().equalsIgnoreCase("yes")) // stream2 - contains only elements with "yes"
    .map(f -> f.getF()+" "+f.getO()+" "+f.getV()) // stream3 - stream of the strings produced by expression f.getF()+" "+f.getO()+" "+f.getV()
    .collect(Collectors.joining(" and "));

Some explanation. Here the .collect(Collectors.joining(...)) joins the elements of the stream produced by the previous .map(f -> f.getF()+" "+f.getO()+" "+f.getV()) call.

There are 3 different streams created:

String query = data.stream() // stream1 - original stream of the data collection
    .filter(f -> f.getData().equalsIgnoreCase("yes")) // stream2 - contains only elements with "yes"
    .map(f -> f.getF()+" "+f.getO()+" "+f.getV()) // stream3 - stream of the strings produced by expression f.getF()+" "+f.getO()+" "+f.getV()
    .collect(Collectors.joining(" and","how to do this" , suffix));

So, first you join f.getF(), f.getO() and f.getV() with " " joiner as follows:

.map(f -> f.getF()+" "+f.getO()+" "+f.getV())

You'll get the stream of the string like

"F1 O1 V1",
"F2 O2 V2",
...

Tis stream will be joined by .collect(Collectors.joining(...));. For example, using .collect(Collectors.joining(" and ")); will produce string like

"F1 O1 V1 and F2 O2 V2 and ..."
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
3

You were almost there:

String query = data.stream()
    .filter(f -> f.getData().equalsIgnoreCase("yes"))
    .map(f -> f.getF() + " " + f.getO() + " " + f.getV()))
    .collect(Collectors.joining(" and "));

Which will produce something like this:

columnName1 = 2 and columnName2 > 5
Lino
  • 19,604
  • 6
  • 47
  • 65