BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String join() method

Last Updated: June 4, 2024 by Chaitanya Singh | Filed Under: java

The join() method of String class is introduced in Java 8. This method is used to concatenate strings with specified delimiter. It is also used to combine multiple elements of a collection with the specified separator. In this guide, we will see several programs to discuss Java String join() method.

Note: Java 8 also introduced a new StringJoiner class for the same purpose.

Syntax

There are two main overloaded versions of the String.join() method:

Joining CharSequence elements

public static String join(CharSequence delimiter, 
CharSequence... elements)

Joining Iterable elements

public static String join(CharSequence delimiter, 
Iterable<? extends CharSequence> elements)

    Parameters

    • delimiter: The delimiter that separates each element, for example comma(,), space(” “) etc. are delimiters.
    • elements: The sequence of elements to be joined.

    Examples of Java String join() method

    1. Joining Strings with a Delimiter

    In the following example, we are concatenating the strings with the specified delimiter.

    public class StringJoinExample {
    public static void main(String[] args) {
    // Here , is a delimiter and elements are strings
    String result = String.join(", ", "Cricket", "Hockey", "Tennis");
    System.out.println(result); // Output: "Cricket, Hockey, Tennis"
    }
    }

    2. Joining Elements of a List (Collection)

    import java.util.Arrays;
    import java.util.List;

    public class StringJoinExample {
    public static void main(String[] args) {
    List<String> sports = Arrays.asList("Cricket", "Hockey", "Tennis");
    // Here, we are joining elements of an ArrayList with
    // comma as delimiter
    String result = String.join(", ", sports);
    System.out.println(result); // Output: "Cricket", "Hockey", "Tennis"
    }
    }

    3. Joining Varargs of Strings

    In the following example, we are using String.join() method with a variable number of string arguments. Refer: Java varargs Tutorial

    public class StringJoinExample {
    public static void main(String[] args) {
    String result = String.join(" | ", "Java", "Python", "C++", "JavaScript");
    System.out.println(result); // Output: "Java | Python | C++ | JavaScript"
    }
    }

    4. Joining Elements of a Collection

    Here, we are using String.join() method to concatenate the elements of a HashSet.

    import java.util.HashSet;

    public class StringJoinHashSetExample {
    public static void main(String[] args) {
    HashSet<String> fruits = new HashSet<>();
    fruits.add("apple");
    fruits.add("banana");
    fruits.add("cherry");
    fruits.add("date");

    // Joining the elements of HashSet with comma as delimiter
    String result = String.join(", ", fruits);

    // Output: "banana, cherry, apple, date"
    // (order can be different)
    System.out.println(result);
    }
    }

    Practical Uses of String join() method

    • CSV Generation: This method can be used to join elements with a comma to generate CSV records.
    • Logging: Concatenating multiple values with a separator for logging purposes.
      ❮ Java String Class

      Top Related Articles:

      1. LinkedHashMap in Java
      2. Java Array Declaration and Initialization
      3. How to Split a String in Java with Delimiter
      4. Java Scanner class with examples
      5. Swing – JButton tutorial and examples

      Tags: Java-Strings

      About the Author

      I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

      – Chaitanya

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Java Tutorial

      Java Introduction

      • Java Index
      • Java Introduction
      • History of Java
      • Features of Java
      • C++ vs Java
      • JDK vs JRE vs JVM
      • JVM - Java Virtual Machine
      • First Java Program
      • Variables
      • Data Types
      • Operators

      Java Flow Control

      • Java If-else
      • Java Switch-Case
      • Java For loop
      • Java while loop
      • Java do-while loop
      • Continue statement
      • break statement

      Java Arrays

      • Java Arrays

      OOPs Concepts

      • OOPs Concepts
      • Constructor
      • Java String
      • Static keyword
      • Inheritance
      • Types of inheritance
      • Aggregation
      • Association
      • Super Keyword
      • Method overloading
      • Method overriding
      • Overloading vs Overriding
      • Polymorphism
      • Types of polymorphism
      • Static and dynamic binding
      • Abstract class and methods
      • Interface
      • Abstract class vs interface
      • Encapsulation
      • Packages
      • Access modifiers
      • Garbage Collection
      • Inner classes
      • Static import
      • Static constructor

      Java Exception Handling

      • Exception handling
      • Java try-catch
      • Java throw
      • Java throws
      • Checked and Unchecked Exceptions
      • Jav try catch finally
      • Exception Examples
      • Exception Propagation

      Collections Framework

      • Collections in Java
      • Java ArrayList
      • Java LinkedList
      • Java Vector
      • Java HashSet
      • Java LinkedHashSet
      • Java TreeSet
      • Java HashMap
      • Java TreeMap
      • Java LinkedHashMap
      • Java Queue
      • Java PriorityQueue
      • Java Deque
      • Comparable interface
      • Comparator interface
      • Collections Interview Questions

      MORE ...

      • Java Scanner Class
      • Java 8 Features
      • Java 9 Features
      • Java Conversion
      • Java Date
      • Java Multithreading
      • Java I/O
      • Java Serialization
      • Java Regex
      • Java AWT
      • Java Swing
      • Java Enum
      • Java Annotations
      • Java main method
      • Java Interview Q

      Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap