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

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

$
0
0

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:

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

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

Author: App Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles