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
1

3 Answers 3

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(" "));
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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
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.
%n is OS independant.
@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.
@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.
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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.