Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. I have been writing front-end code for a long time. I have forgotten a lot of back-end code. I wanted to write something advanced, but I still like to write some gadgets.
Suddenly, IT occurred to me that I had used a similar tool class before, so I reviewed the old and learned something new
Pom. The XML file
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Hanpang < / groupId > < artifactId > 01 - Guava - Joiner < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < dependencies > <! -- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> < artifactId > guava < / artifactId > < version > 31.0 jre < / version > < / dependency > <! -- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies> </dependencies> </project>Copy the code
Test code file
import com.google.common.base.Joiner; import com.google.common.io.Files; import org.junit.Assert; import org.junit.Test; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; import static com.google.common.collect.ImmutableMap.of; /** * https://www.yiibai.com/guava/guava_joiner.html */ public class JoinerTest { public final List<String> bookList = AsList (" Journey to the West ", "A Dream of red Mansions "," Romance of The Three Kingdoms ", "Water Margin "); Public final List<String> authorList = Array. AsList (" cZ ", "CZ ", NULL," CZ ", "CZ ", null); /** * 1. Join (array) and (collection) with join (array). <result> */ @test public void testJoinOnJoin() {String result = joiner.on ("##").join(bookList); System.out.println(result); AssertEquals (result, "Journey to the West ## Dream of red Mansions ## Romance of The Three Kingdoms ## Water Margin "); } /** * 1. You can use join (array) and (collection) to set the delimiter. If (array) and (collection) contain NULL, a NullPointerException is thrown. * 3. Return after joining together of a String. < result > * / @ Test (expected = NullPointerException. Class) public void testJoinOnJoinWithNullValue () {String result = Joiner.on("##").join(authorList); System.out.println(result); AssertEquals (result, "Wu Chengen ## Cao Xueqin ## Luo Guanzhong ## Shi Naian "); } /** * 1. You can use join (array) and (collection) to set the delimiter. If (array) and (collection) contain NULL, a NullPointerException is thrown. * 3. SkipNulls () can be used to remove null. Return after joining together of a String. < result > * / @ Test public void testJoinOnJoinWithNullValueButSkip () {String result = Joiner.on("##").skipNulls().join(authorList); System.out.println(result); AssertEquals (result, "Wu Chengen ## Cao Xueqin ## Luo Guanzhong ## Shi Naian "); } /** * 1. You can use join (array) and (collection) to set the delimiter. If (array) and (collection) contain NULL, a NullPointerException is thrown. * 3. Use useForNull() to replace null. * 4. Return after joining together of a String. < result > * / @ Test public void testJoin_On_Join_WithNullValueUseDefaultValue () {String result = Joiner. On (" # # "). UseForNull (" fat, "first). Join (authorList); System.out.println(result); AssertEquals (result, "Wu Chengen ## Cao Xueqin ## Fat Sen ## Luo Guanzhong ## Shi Naian ## Fat Sen "); } /** * 1. You can use join (array) and (collection) to set the delimiter. If (array) and (collection) contain NULL, a NullPointerException is thrown. * 3. You can use skipNulls(), <result> */ @test public void void <result> */ @test public void test_joiner_append_to_StringBulider() { StringBuilder builder = new StringBuilder(); StringBuilder resultBuilder = Joiner.on("##").skipNulls().appendTo(builder, authorList); Assert.assertEquals(builder, resultBuilder); AssertEquals (resultBuilding.toString (), "Wu Chengen ## Cao Xue-qin ## Luo Guan-zhong ## Shi Nean "); } /** * 1. You can use join (array) and (collection) to set the delimiter. If (array) and (collection) contain NULL, a NullPointerException is thrown. * 3. You can use skipNulls(), <result> */ @test public void test_Joiner_Append_Writer() {String targetFileName = "D:/append.txt"; try(FileWriter writer = new FileWriter(targetFileName); ) { Joiner.on("##").skipNulls().appendTo(writer, authorList); Assert.assertEquals(Files.isFile().test(new File(targetFileName)),true); }catch (IOException ex){assert. fail(" write data failed "); }} /*======================= Don't use Guava for Guava's sake, We Java8 also can realize the above content = = = = = = = = = = = = = = = = = = = = = = = = = * / @ Test public void testJoinBySteam () {String result = authorList.stream().filter(item -> Objects.nonNull(item) && ! item.isEmpty()).collect(Collectors.joining("##")); AssertEquals (result, "Wu Chengen ## Cao Xueqin ## Luo Guanzhong ## Shi Naian "); } @Test public void testJoinBySteamWithDefaultValue(){ String result = authorList.stream().map(item -> Objects.isNull(item) || item.isEmpty()?" Fat first sen ": the item). Collect (Collectors. Joining (" # #")); AssertEquals (result, "Wu Chengen ## Cao Xueqin ## Fat Sen ## Luo Guanzhong ## Shi Naian ## Fat Sen "); } private final Map<String,String> stringMap = of("Java","PHP","JavaScript","Python"); @Test public void testJoinOnWithMap(){ String result = Joiner.on("%").withKeyValueSeparator("->").join(stringMap); System.out.println(result); } @Test public void test_Joiner_Append_Writer_Map() { String targetFileName = "D:/appendMap.txt"; try(FileWriter writer = new FileWriter(targetFileName); ) { Joiner.on("%").withKeyValueSeparator("->").appendTo(writer, stringMap); }catch (IOException ex){assert. fail(" write data failed "); }}}Copy the code