Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java StringJoiner

  • In the java.util.stream package, a new class called Collectors was added in Java 8. This class offers a number of static methods that can be used to carry out different reduction operations on a stream's elements.

StringJoiner Constructors

  • StringJoiner(CharSequence delimiter) : It creates a StringJoiner with a duplicate of the supplied delimiter and no characters, prefixes, or suffixes.
  • public StringJoiner(CharSequence delimiter)

    NullPointerException Exception Thrown if the delimiter is null





  • StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) : It creates a StringJoiner devoid of characters by duplicating the supplied prefix, delimiter, and suffix. It will return the prefix + suffix (or properties thereof) in the result if no characters are added to the StringJoiner and methods accessing the string value are called, unless setEmptyValue has been called first.
  • public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

    NullPointerException Exception Thrown if prefix, delimiter, or suffix is null.



Methods of StringJoiner Class

Method Action Performed
add() Adds a duplicate of the specified CharSequence value as the StringJoiner value's subsequent element. In that case, "null" is added if newElement is null.
length() This StringJoiner's String representation's length is returned.
merge() Adds the given StringJoiner's contents, without a prefix or a suffix, as the following element if it is not empty. The call has no impact if the provided StringJoiner is empty. Let's say that the other StringJoiner employs a different delimiter. In that case, the result is appended to this StringJoiner as a single element by concatenating the elements from the other StringJoiner with that delimiter.
toString() it returns the String object for this StringJoiner.
setEmptyValue() sets the string that will be used to determine the string representation of this StringJoiner when no elements have been added yet, or when it is empty.





A Java program that displays the StringJoiner class's methods

import java.util.StringJoiner;

public class Main {
    public static void main(String[] args) {
        // Creating a StringJoiner object with delimiter ","
        StringJoiner sj1 = new StringJoiner(",");

        // Using setEmptyValue() method to set default value for empty StringJoiner object
        sj1.setEmptyValue("sj1 is empty");
        System.out.println(sj1); // sj1 is empty

        // Using add() method to add elements to StringJoiner object
        sj1.add("Ram").add("Jyoti").add("Alice").add("Shyam");
        System.out.println(sj1); // Ram,Jyoti,Alice,Shyam

        // Using length() method to get length of the StringJoiner object
        System.out.println("Length of sj1 : " + sj1.length()); // Length of sj1 : 17

        // Creating a new StringJoiner object with delimiter ":"
        StringJoiner sj2 = new StringJoiner(":");
        sj2.add("John").add("Jani").add("Jimmy");

        // Using merge() method to merge two StringJoiner objects
        sj1.merge(sj2);

        // Using toString() method to get the String representation of the StringJoiner object
        System.out.println(sj1.toString()); // Ram,Jyoti,Alice,Shyam,John:Jani:Jimmy

        // Using length() method to get length of the new StringJoiner object after merging
        System.out.println("Length of new sj1 : " + sj1.length()); // Length of new sj1 : 26
    }
}




This program creates two StringJoiner objects, adds elements to them using the add() method, sets a default value for the first object using setEmptyValue(), merges the two objects using the merge() method, and finally, gets the String representation of the merged object using the toString() method and gets its length using the length() method.


Output:

sj1 is empty
Ram,Shyam,Alice,Bob
Length of sj1 : 17
Ram,Shyam,Alice,Bob,John:Jane:Jim
Length of new sj1 : 26 

Java Optional Class Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.