-3

I want a way to iterate over a list without actual using values of it and creating a parameterized String with "?" for Query.

 for(int i = 0; i<list.size(); i++)
 {
    spQuery = spQuery + "?,";
 }

I want to convert above code to Java 8 Streams code. Final Answer should be something like this - ?,?,?,?...n where n is size of the list

jps
  • 20,041
  • 15
  • 75
  • 79
Nitin Rathod
  • 159
  • 2
  • 15
  • Why do you want to turn this into a stream? What's wrong with your current approach? – QBrute Nov 03 '21 at 11:13
  • 3
    How about `spQuery = spQuery +"?,".repeat(list.size())`? – Pshemo Nov 03 '21 at 11:13
  • Btw, using += inside a loop to build a string is FORBIDDEN – stephane brun Nov 03 '21 at 11:21
  • @stephanebrun Note there are two kinds of `+` (1) addition and (2) concatenation. – Pshemo Nov 03 '21 at 11:24
  • @Hulk "*…I want to convert above code to Java 8 Streams*" requirement prevents me from posting it as an answer. – Pshemo Nov 03 '21 at 12:00
  • @Pshemo agreed. My proposed duplicate provides a stream solution and a `String.repeat` solution, so I guess there is no need to add another answer. – Hulk Nov 03 '21 at 12:03
  • Also related: https://stackoverflow.com/questions/3107044/preparedstatement-with-list-of-parameters-in-a-in-clause – Hulk Nov 03 '21 at 12:12

3 Answers3

0

Streams shouldn't be used just for the stream's sake. They're a powerful tool when it comes to transforming data in a fluent way, but for simple tasks like this they are overkill and the code will be harder to understand.

You could potentially do this with

String s = Stream.generate(() -> "?").limit(list.size()).collect(Collectors.joining(","));

But compare it to the much easier version that you already have:

for(int i = 0; i<list.size(); i++) {
    spQuery = spQuery + "?,";
}

Here you can see at a glance what you want to do without the need to dissect the stream expression.

QBrute
  • 4,405
  • 6
  • 34
  • 40
0

If your List elements are of type String, it should be

String s = list.stream().collect(Collectors.joining("?,"));

but then, the last element is missing "?," so if you also need the question mark after the last element, you end up with

String s = list.stream().collect(Collectors.joining("?,")).concat("?,");

In general, if you have multiple appends/concatenations to a String, it's way more performant to use a StringBuffer or StringBuilder (String is an immutable Object).

If the list elements are of a different type than String, you have to map your object to a String representation, like

String s = list.stream().map(listElement -> <<some code that returns a String representation of the element>>)).collect(Collectors.joining("?,")).concat("?,");
The Frozen One
  • 281
  • 2
  • 10
0

You might be trying to do the following:

String spQuery = list.stream().map(e -> "?").collect(Collectors.joining(","));

As you don't need the list's elements, I would prefer:

String spQuery = IntStream.range(0, list.size()).mapToObj(i -> "?").collect(Collectors.joining(","));

You can probably store the "?" in a variable before to avoid instantiating it each time.

However, as everyone else has said, you shouldn't use streams just because you can. If it gives no benefit, then, why?

You could just use a StringJoiner with a for loop to add it a number of times.

dajavax
  • 3,060
  • 1
  • 14
  • 14