Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Java StringJoiner, String.join() and Collectors.joining() Tutorial with all Details – 5 Different ways

$
0
0

Java8 StringJoiner, String.join() and Collectors.joining() Tutorial with all Details - 5 Different ways - Crunchify Tips

Java Developer Platform 8 (Java8) is around since almost 3 years. We have published more than 10 different tutorials on Crunchify with all different abilities and handy utilities.

Moving from one version to new version is not always smooth and easy for big companies. There might be more than tens of thousands of VMs (Hosts) which runs production application. Java Architect or Project Architect might have to look at all possible enhancements of new version of Java and then decide on moving to new Java version.

Changes into production application may need some extra bandwidth from developer and sometimes takes couple of months. StringJoiner is one of the great utility for any Java Developer in additional to below utils.

In this tutorial we will go over StringJoiner(), String.join() and Collectors.joining() utilities.

Here is Java 8 – StringJoiner example.

Let’s get started:

  1. Create Java class CrunchifyJava8StringJoinerTutorial.java
  2. We will perform below Join Operations
    1. Simple Delimiter Join
    2. Join using Prefix and Suffix
    3. Join using String Join
    4. Join using ListJoiner
    5. Collection Joiner
  3. Print all results in Eclipse console

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

CrunchifyJava8StringJoinerTutorial.java

package crunchify.com.tutorial;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * 
 * @author Crunchify.com 
 * List of all possible Java8 String Joiner Examples 
 * StringJoiner(), String.join() and Collectors.joining() utilities
 * 
 */

public abstract class CrunchifyJava8StringJoinerTutorial {

	public static void main(String[] args) {

		// StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a
		// supplied prefix and ending with a supplied suffix.

		// Simple Delimiter Join
		crunchifyJava8JoinDelimiter();

		// Join using Prefix and Suffix
		crunchifyJava8StringJoinerWithPrefixSuffix();

		// Join using String Join
		crunchifyJava8SimpleStringJoin();

		// Join using ListJoiner
		crunchifyJava8ListJoiner();

		// Collection Joiner
		crunchifyJava8CollectorJoiner();

	}

	private static void crunchifyJava8JoinDelimiter() {
		StringJoiner myPhone = new StringJoiner(".");
		myPhone.add("123");
		myPhone.add("456");
		myPhone.add("7890");
		String phoneNumber = myPhone.toString();
		log("1. StringJoiner with simple delimiter: \t" + phoneNumber);

	}

	private static void crunchifyJava8StringJoinerWithPrefixSuffix() {

		// Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.
		StringJoiner myDate = new StringJoiner("-", "{", "}");
		myDate.add("1985");
		myDate.add("11");
		myDate.add("22");
		String birthDate = myDate.toString();
		log("2. StringJoiner with Prefix and Suffix: " + birthDate);

	}

	private static void crunchifyJava8SimpleStringJoin() {

		// Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified
		// delimiter.
		String crunchifyFoundDate = String.join("/", "2012", "07", "01");
		log("3. Simple String Join: \t\t\t" + crunchifyFoundDate);

	}

	private static void crunchifyJava8ListJoiner() {
		List<String> companyList = Arrays.asList("Crunchify LLC", "Google Inc", "Facebook Inc", "Amazon LLC");

		String companies = String.join(", ", companyList);
		log("4. Join List with Delimiter: \t\t" + companies);

	}

	private static void crunchifyJava8CollectorJoiner() {
		List<String> tutorialList = Arrays.asList("Spring MVC", "Java", "NodeJS", "C Sharp");

		// joining: returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter
		// order.
		String tutorials = tutorialList.stream().map(crunchify -> crunchify).collect(Collectors.joining(" <==> "));

		log("5. Collector Joining: \t\t\t" + tutorials);
	}

	private static void log(String data) {
		System.out.println(data);

	}

}

Eclipse Console Result:

Now simply run above program as Java Application and you will see result similar to this one.

1. StringJoiner with simple delimiter: 	123.456.7890
2. StringJoiner with Prefix and Suffix: {1985-11-22}
3. Simple String Join: 			2012/07/01
4. Join List with Delimiter: 		Crunchify LLC, Google Inc, Facebook Inc, Amazon LLC
5. Collector Joining: 			Spring MVC <==> Java <==> NodeJS <==> C Sharp

I hope you get a complete idea on how to use String Joiner starting Java 8 onwards.

What’s Next?

Have you noticed Java 11 limitation? What things Java 11 removed from it’s JDK? Check this tutorial out.

Java 11 and How to fix java.lang. TypeNotPresentException: Type javax.xml.bind.JAXBContext Exception?

The post Java StringJoiner, String.join() and Collectors.joining() Tutorial with all Details – 5 Different ways appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles