The join()
method in Java is used to join multiple strings into a single string. It is a static method that is available in the String
class since Java 8. The method takes two parameters: the delimiter and an array or iterable of strings to be joined.
Here’s the syntax for using the join()
method:
String joinedString = String.join(delimiter, string1, string2, ..., stringN);
or
String joinedString = String.join(delimiter, iterable);
where:
delimiter
is the string used to separate the joined strings.string1
,string2
, …,stringN
are the strings to be joined.iterable
is an object that implements theIterable
interface, such as aList
or aSet
, containing the strings to be joined.
Here’s an example usage of the join()
method:
String[] languages = {"Java", "Python", "JavaScript", "C++", "Ruby"}; String joinedLanguages = String.join(", ", languages); System.out.println(joinedLanguages);
Output:
Java, Python, JavaScript, C++, Ruby
In the above example, the join()
method is used to join the elements of the languages
array with a comma and a space as the delimiter. The resulting string is assigned to the joinedLanguages
variable and printed to the console.
Signature of Java String join():
There are two overloaded versions of the join()
method in the Java String class. Here are their signatures:
public static String join(CharSequence delimiter, CharSequence... elements)
This version of the method takes a CharSequence
delimiter as the first argument and one or more CharSequence
elements as the remaining arguments. It returns a String
that is the concatenation of the elements
, separated by the delimiter
.
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
This version of the method takes a CharSequence
delimiter as the first argument and an Iterable
of CharSequence
elements as the second argument. It returns a String
that is the concatenation of the elements
, separated by the delimiter
.
In both cases, the CharSequence
type is an interface implemented by the String
class, which means that the join()
method can be used with any class that implements this interface, including the String
class itself.
Parameters of Java String join():
The join()
method in Java takes two parameters:
delimiter
– This is aCharSequence
object that specifies the delimiter to be used when joining the strings. The delimiter can be any sequence of characters, including an empty string.elements
– This is either an array or anIterable
object containing the strings to be joined. Each element in the array orIterable
must be aCharSequence
, which includes theString
class and any other class that implements theCharSequence
interface.
The join()
method concatenates the elements in the elements
array or Iterable
object, using the delimiter
as the separator between the elements, and returns the resulting string.
Note that the first parameter, delimiter
, is always required, while the second parameter, elements
, can be either an array or an Iterable
object.
Returns of Java String join():
The join()
method in Java returns a String
that is the concatenation of the elements in the elements
array or Iterable
object, separated by the delimiter
.
Here’s an example usage of the join()
method:
String[] colors = {"red", "green", "blue"}; String joinedColors = String.join(", ", colors); System.out.println(joinedColors);
Output:
red, green, blue
In this example, the join()
method concatenates the strings “red”, “green”, and “blue” from the colors
array, using the string “, ” as the delimiter, and returns the resulting string “red, green, blue”. This string is then assigned to the joinedColors
variable and printed to the console.
Internal Implementation:
Internally, the join()
method in Java uses a StringBuilder
to build the resulting string by appending the elements from the elements
array or Iterable
object, separated by the delimiter
.
Here’s an example of how the join()
method could be implemented using a StringBuilder
:
public static String join(CharSequence delimiter, CharSequence... elements) { // Create a StringBuilder to build the resulting string StringBuilder sb = new StringBuilder(); // Append the first element to the StringBuilder sb.append(elements[0]); // Append the remaining elements, separated by the delimiter for (int i = 1; i < elements.length; i++) { sb.append(delimiter).append(elements[i]); } // Return the resulting string return sb.toString(); }
In this implementation, the StringBuilder
is used to concatenate the elements in the elements
array, starting with the first element. Then, a loop is used to append each subsequent element, separated by the delimiter
. Finally, the resulting string is returned by calling the toString()
method on the StringBuilder
.
The implementation of the join()
method that takes an Iterable
as its second argument is similar, except that it uses an iterator to iterate over the elements in the Iterable
instead of an array index.
Java String join() Method Example:
Sure, here’s an example of using the join()
method in Java:
import java.util.Arrays; public class Example { public static void main(String[] args) { // Join an array of strings with a delimiter String[] words = {"Hello", "world", "how", "are", "you"}; String sentence = String.join(" ", words); System.out.println(sentence); // Join a list of strings with a delimiter List<String> colors = Arrays.asList("red", "green", "blue"); String colorList = String.join(", ", colors); System.out.println(colorList); } }
Output:
Hello world how are you red, green, blue
In this example, we use the join()
method to concatenate an array of strings and a list of strings, separated by a delimiter.
In the first part of the example, we define an array of strings called words
, and use the join()
method to concatenate the strings in the array, separated by a space. The resulting string is assigned to the sentence
variable, which is then printed to the console.
In the second part of the example, we define a list of strings called colors
, and use the join()
method to concatenate the strings in the list, separated by a comma and a space. The resulting string is assigned to the colorList
variable, which is then printed to the console.
Note that we use the Arrays.asList()
method to create a List
object from the array of strings, which allows us to use the join()
method with the list.