Home » Java programming language

Differences between StringJoiner and StringBuilder in Java 8

StringJoiner vs StringBuilder in Java 8: Here, we are going to learn about the differences between StringJoiner and StringBuilder in Java 8.
Submitted by Preeti Jain, on July 30, 2019

StringJoiner vs StringBuilder

Here, we will see the differences between StringJoiner and StringBuilder.

1) StringJoiner

  • StringJoiner is a class which is available in java.util package.
  • StringJoiner is a class used to concatenate strings.
  • StringJoiner class provides add(String str) method to join the strings based on a given delimiter, prefix, and suffix in the constructor simultaneously.
  • StringJoiner is a final class and we can't override this class.
  • In StringJoiner constructor we pass we can pass three arguments like String delimiter, String prefix, and String suffix.

Syntax:

    StringJoiner object_name = new StringJoiner(delimeter,prefix,suffix);

Problem: Let suppose we are expecting the string like "{"Java"; "C"; "C++"; "Ruby"}" where we have given a string array that contains "Java" , "C" , "C++" , "Ruby"

Example:

import java.util.StringJoiner;

public class StringJoinerClass {
    public static void main(String[] args) {
        // Creating a constructor of StringJoiner class
        StringJoiner strj = new StringJoiner(";", "{", "}");

        // add few elements to StringJoiner for concatenation 
        // by using add(String str) method
        strj.add("Java");
        strj.add("C");
        strj.add("C++");
        strj.add("Ruby");

        // Display concatenation of strings by StringJoiner class
        System.out.println("The result after concatenation is :" + strj);
    }
}

Output

The result after concatenation is :{Java;C;C++;Ruby}

We will see how StringBuilder differs from StringJoiner class?

2) StringBuilder

  • StringBuilder is a class which is available in java.lang package.
  • StringBuilder is also class which provides the facility to concatenate the strings.
  • StringBuilder class provides append( String str) method to append the string.
  • In StringBuilder class constructor we don't pass such kind of argument like String delimiter, String prefix, String suffix.
  • StringBuilder is not a final class so we can't override this class.

Syntax:

    StringBuilder strb = new StringBuilder();

Problem: Let suppose we are expecting the string like "{"Java"; "Programming"}" where we have given a string array that contains "Java" , "Programming".

We will see what methodology follow to solve this problem by using the StringBuilder class?

  • First, we will append prefix "{" by using append(String str) method.
  • Second, we will traverse string array {"Java", "Programming"} and append delimiter after every element.
  • Finally, we will append suffix "}".

Example:

import java.lang.StringBuilder;

class StringBuilderClass {
    public static void main(String[] args) {
        // Creating a constructor of StringBuilder 
        // class with passing no argument
        StringBuilder strb = new StringBuilder();

        // add few elements to StringJoiner for concatenation 
        // by using add(String str) method

        // First we will append prefix if we want
        strb.append("{");

        // Second we will append required delimeter after every element
        strb.append("Java");
        strb.append(";").append("C");
        strb.append(";").append("C++");

        // Finally we will append suffix if we want
        strb.append("}");

        // Display concatenation of strings by StringBuilder class
        System.out.println("The result after concatenation is :" + strb);
    }
}

Output

E:\Programs>javac StringBuilderClass.java

E:\Programs>java StringBuilderClass
The result after concatenation is :{Java;C;C++}






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.