Wednesday 30 December 2015

StringJoiner class

StringJoiner class is newly added in Java8, included under the package java.util. By using StringJoiner class, you can concatenate strings in easy way.


For Example,
import java.util.StringJoiner;

public class StringJoinerDemo {
    public static void main(String args[]) {
        StringJoiner joiner = new StringJoiner(",");

        String[] mountains = { "Mount Everest", "K2", "Kangchenjunga",
                "Lhotse", "Makalu", "Cho Oyu", "Dhaulagiri I" };

        for (String s : mountains) {
            joiner.add(s);
        }

        String result = joiner.toString();

        System.out.println("result = " + result);
    }
}


Output
result = Mount Everest,K2,Kangchenjunga,Lhotse,Makalu,Cho Oyu,Dhaulagiri I


StringJoiner joiner = new StringJoiner(",");
Create a new instance of StringJoiner class and use , as delimeter to separate strings.

‘Joiner.add(s)’ append string ‘s’ as the next element of the StringJoiner value.

StringJoiner class provide another constructor, which takes delimeter, prefix and suffix strings as arguments and constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.

import java.util.StringJoiner;

public class StringJoinerDemo {
    public static void main(String args[]) {
        StringJoiner joiner = new StringJoiner(",", "{", "}");

        String[] mountains = { "Mount Everest", "K2", "Kangchenjunga",
                "Lhotse", "Makalu", "Cho Oyu", "Dhaulagiri I" };

        for (String s : mountains) {
            joiner.add(s);
        }

        String result = joiner.toString();

        System.out.println("result = " + result);
    }
}


Output

result = {Mount Everest,K2,Kangchenjunga,Lhotse,Makalu,Cho Oyu,Dhaulagiri I}


Observe the output, prefix ‘{‘ and suffix ‘}’ is added to the final result.

Merge two StringJoiner instances
‘merge(StringJoiner other)’ adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.

import java.util.StringJoiner;

public class StringJoinerDemo {
    static void addToJoiner(StringJoiner joiner, String[] input) {
        for (String s : input) {
            joiner.add(s);
        }
    }

    public static void main(String args[]) {
        StringJoiner evenJoiner = new StringJoiner(",", "{", "}");
        StringJoiner oddJoiner = new StringJoiner(":", "(", ")");
        StringJoiner primeJoiner = new StringJoiner("$", "[", "]");
        StringJoiner allJoiner = new StringJoiner(" # ");

        String[] evenNumbers = { "2", "4", "6", "8" };
        String[] oddNumbers = { "1", "3", "5", "7", "11", "13", "17" };
        String[] primeNumbers = { "2", "3", "5", "7", "11" };

        addToJoiner(evenJoiner, evenNumbers);
        addToJoiner(oddJoiner, oddNumbers);
        addToJoiner(primeJoiner, primeNumbers);

        System.out.println("evenJoiner = " + evenJoiner);
        System.out.println("oddJoiner = " + oddJoiner);
        System.out.println("primeJoiner = " + primeJoiner);

        System.out.println("Before merge allJoiner = " + allJoiner);
        allJoiner.merge(evenJoiner).merge(oddJoiner).merge(primeJoiner);
        System.out.println("After merge allJoiner = " + allJoiner);
    }
}


Output

evenJoiner = {2,4,6,8}
oddJoiner = (1:3:5:7:11:13:17)
primeJoiner = [2$3$5$7$11]
Before merge allJoiner = 
After merge allJoiner = 2,4,6,8 # 1:3:5:7:11:13:17 # 2$3$5$7$11


Get the length of StringJoiner
StringJoiner class method provide 'length()' method, which return number characters in the String representation of StringJoiner. if no add methods have been called, then the length of the String representation is either prefix + suffix or emptyValue.

import java.util.StringJoiner;

public class StringJoinerDemo {
    public static void main(String args[]) {
        StringJoiner joiner1 = new StringJoiner(",");
        StringJoiner joiner2 = new StringJoiner(",", "{", "}");

        System.out.println(joiner1 + " : " + joiner1.length());
        System.out.println(joiner2 + " : " + joiner2.length());

        joiner1.add("Hello");
        joiner2.add("Hello World");

        System.out.println(joiner1 + " : " + joiner1.length());
        System.out.println(joiner2 + " : " + joiner2.length());
    }
}


Output

: 0
{} : 2
Hello : 5
{Hello World} : 13


The value returned by length of joiner us equivalent to joiner.toString().length().




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment