Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UsingStringBuilder.java #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions strings/UsingStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
// Visit http://OnJava8.com for more book information.
import java.util.*;
import java.util.stream.*;

public class UsingStringBuilder {
public static String string1() {
Random rand = new Random(47);
StringBuilder result = new StringBuilder("[");
for(int i = 0; i < 25; i++) {
StringBuilder result = new StringBuilder("[");
for (int i = 0; i < 25; i++) {
result.append(rand.nextInt(100));
result.append(", ");
}
result.delete(result.length()-2, result.length());
result.append("]");
}
result.delete(result.length() - 2, result.length()); // Remove the extra comma and space
result.append("]");
return result.toString();
}
}
public static String string2() {
String result = new Random(47)
.ints(25, 0, 100)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));
return "[" + result + "]";
.ints(25, 0, 100) // Generate 25 random integers between 0 and 99
.mapToObj(Integer::toString) // Convert each integer to its string representation
.collect(Collectors.joining(", ")); // Join the strings with a comma and space separator
return "[" + result + "]"; // Wrap the result in square brackets
}
public static void main(String[] args) {
System.out.println(string1());
Expand Down