复制代码

为懒人提供无限可能,生命不息,code不止

人类感性的情绪,让我们知难行难
我思故我在
日拱一卒,功不唐捐
  • 首页
  • 前端
  • 后台
  • 数据库
  • 运维
  • 资源下载
  • 实用工具
  • 接口文档工具
  • 登录
  • 注册

JAVA代码

【原创】JAVA 8(JDK8)StringJoiner 示例 String Join ListJoiner

作者: whooyun发表于: 2017-03-29 00:07

本示例主要包括以下操作:
1、分隔符连接操作;
2、前缀和后缀连接操作;
3、String Join 连接操作;
4、ListJoiner 连接操作;
5、Collection Joiner;



StringJoiner 通过分隔符,字符前缀,字符后缀来分隔,连接字符
类似于
StringBuffer strbuf = new StringBuffer("123");
strbuf.append(".");
strbuf.append("456");
strbuf.append(".");

strbuf.append("789");

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);
 
	}
 
}