2

Lets say I have a method which returns a list with a random number of random numbers - we will call that method List<Integer> getRandomNumberOfRandomNumbers();

System.out.println(String.format("Here are the random numbers: %s", getRandomNumberOfRandomNumbers()));

I know this won't work but how can I achieve this effect?

Example Output

Here are the random numbers:

1 4 2 4

Here are the random numbers:

2 43 323 434 3423 54
Ogen
  • 6,499
  • 7
  • 58
  • 124

3 Answers3

4

It's easiest to just make getRandomNumberOfRandomNumbers return all of the numbers in a single String. Or you could do it all inline, like this:

getRandomNumberOfRandomNumbers().stream()
    .map(Object::toString)
    .collect(Collectors.joining(" "));
Jeremy Gurr
  • 1,613
  • 8
  • 11
3

For example, if we have a List of Integers as below:

List<Integer> var = new ArrayList<>();
var.add(1);
var.add(4);
var.add(2);
var.add(4);

Then to print the List in your desired way, you can follow this:

System.out.println(String.format("Here are the random numbers:%n%n%s",
                             var.toString().replaceAll("[,\\[,\\]]", "")));

This outputs:

Here are the random numbers:

1 4 2 4

Here var is the list your function getRandomNumberOfRandomNumbers() is returning and we are converting it to String using var.toString() but it returns [1, 4, 2, 4]. So, we need to remove [, ] and ,. I used regular expression to replace them with an empty character. This is actually simple and easy way but not sure whether it is an efficient way!! Whenever i need to convert a Colletion to String, i use toString() method and then do some trick with regular expression to get my desired form of representation.

Update: After surfing web, i found few alternatives. (Preferred over my previous answer using List.toString())

For example, in your case, you can do:

StringJoiner sj = new StringJoiner(" ");
for (Integer i : var) {
    sj.add(String.valueOf(i));
}
System.out.println(sj.toString());

You can do the same with the following two alternatives.

Community
  • 1
  • 1
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • it's bad to rely on the implementation of the List's toString like that. It may change depending on implementation. Also you should use `%n` not `\n` – Patrick Parker Dec 08 '16 at 04:54
  • is there any reason to prefer `%n` over `\n`? and when you said List's `toString()` can be changed, did you mean if java API changes? i guess the probability is pretty low and yes off course, we can change our code if someday `toString()` returns different string representation. – Wasi Ahmad Dec 08 '16 at 04:59
  • 2
    `%n` is OS independant. – Scary Wombat Dec 08 '16 at 05:15
  • @WasiAhmad when you are working on a large team with different areas of responsibility, you cannot rely on List returned by someone else's method to always remain an ArrayList. In the future they may use a custom version of AbstractList that overrides the default toString. Assumptions like this are a bad habit. – Patrick Parker Dec 08 '16 at 06:58
  • @PatrickParker i agree with you. i didn't think in that way. i appreciate your explanation and frankly speaking after your comment, i have updated my answer with more reasonable ways. if you have given me downvote for that reason, i urge you to reconsider. – Wasi Ahmad Dec 08 '16 at 07:11
0

Another solution is iterating through the list and adding the values to a string builder before printing it out

like so:

StringBuilder stringBuilder = new StringBuilder();
for(Integer number : getRandomNumberOfRandomNumbers())
    stringBuilder.append(number + " ");
System.out.println("Here are the random numbers: " + stringBuilder.toString().trim());