Last active
August 29, 2015 14:16
-
-
Save up1/4d77f6a675160b790fe6 to your computer and use it in GitHub Desktop.
Java8 :: String Join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final String COMMA_DELIMITER = ","; | |
//แบบที่ 1 | |
String[] names = {"I", "love", "You"}; | |
System.out.println( String.join(COMMA_DELIMITER, names) ); | |
//แบบที่ 2 | |
System.out.println(String.join(COMMA_DELIMITER, "I", "love", "You")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> inputs = new ArrayList<String>(); | |
inputs.add("I"); | |
inputs.add("love"); | |
inputs.add("You"); | |
System.out.println(String.join(COMMA_DELIMITER, inputs)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StringJoiner joiner = new StringJoiner(":", "[", "]"); | |
joiner.add("192"); | |
joiner.add("168"); | |
joiner.add("1"); | |
joiner.add("234"); | |
System.out.println(joiner.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> inputs = new ArrayList<String>(); | |
inputs.add("I"); | |
inputs.add("love"); | |
inputs.add("You"); | |
String output = inputs.stream().map( messgae -> messgae.toString()).collect(Collectors.joining(COMMA_DELIMITER)); | |
System.out.println( output ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> datas = Arrays.asList("192", "168", "1", "234"); | |
String output = datas.stream().map( messgae -> messgae.toString()).collect(Collectors.joining(":", "[", "]")); | |
System.out.println( output ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment