Java8ã®æååçµå
Java8ããæååã®çµåæ¹æ³ãããã¤ã追å ãããã®ã§ãç°¡åã«ã¾ã¨ãã¾ããã
StringJoiner
Java8ããæ°ãã追å ãããã¯ã©ã¹java.util.StringJoinerã
åºåãæåããµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ãåãã¦æååãé£çµã§ããã®ã§
便å©ã©ã¤ãã©ãªGuavaã®Joiner#onã®ä»£ããã«ãªãæãã§ããããã
ãµã³ãã«ã³ã¼ã
public static void main(String[] args) { ãã// åç´ã«æååã®çµå StringJoiner sj = new StringJoiner(""); sj.add("a"); sj.add("b"); sj.add("c"); ãã// sj.add("a").add("b").add("c"); ã§ãOK System.out.println(sj); //abc ãã// åºåãæåã追å ãã¦æååã®çµå StringJoiner sj2 = new StringJoiner(":"); sj2.add("a"); sj2.add("b"); sj2.add("c"); ãã// sj2.add("a").add("b").add("c"); ã§ãOK System.out.println(sj2); //a:b:c ãã// ãµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ã追å ãã¦ã®ã«æååã®çµå StringJoiner sj3 = new StringJoiner(":", "[", "]"); sj3.add("1"); sj3.add("2"); sj3.add("3"); ãã// sj3.add("1").add("2").add("3"); ã§ãOK System.out.println(sj3); //[1:2:3] ãã// ãã¼ã¸ StringJoiner sj4 = sj2.merge(sj3); System.out.println(sj4); //a:b:c:1:2:3 ãã// ãã¼ã¸ã2ã é çªãéã«ããã¨ãµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ã追å ãããã®ã§æ³¨æ sj4 = sj3.merge(sj2); System.out.println(sj4); //[1:2:3:a:b:c] ãã// ãã¼ã¸ã3ã ãµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ãéè¤ãã¦è¡¨ç¤ºããããã¨ã¯ãªã sj4 = sj3.merge(sj3); System.out.println(sj4); //[1:2:3:1:2:3] // 空ã®å ´åã®è¨å® StringJoiner sj5 = new StringJoiner(":"); sj5.setEmptyValue("It's EMPTY!!"); System.out.println(sj5); //It's EMPTY!! }
String#join
StringJoineråæ§æååã®é£çµã«åºåãæåã追å ã§ããã
ãã ãStringJoinerã¨éã£ã¦ãµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ã®æå®ã¯ã§ããªãã®ãæ®å¿µã
ãµã³ãã«ã³ã¼ã
public static void main(String[] args) { ãã// åºåãæåã追å ãã¦æååã®çµå String str = String.join(":", "a","b","c"); System.out.println(str); //a:b:c ãã// Listãå¼æ°ã«çµå ããList<String> list = Arrays.asList("a", "b", "c"); ããString str2 = String.join(":", list); ããSystem.out.println(str2); //a:b:c }
Collectors#joining
Stream#collectã¡ã½ããã®å¼æ°ã«æ¸¡ãjava.util.stream.Collectors ã®joiningã¡ã½ããã
Collectorã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å
·è±¡ã¯ã©ã¹ã
joining以å¤ã«ãaveragingIntã¨ãgroupingByã¨ãã£ã便å©ãªãã®ãããããç¨æããã¦ããã
http://www.ne.jp/asahi/hishidama/home/tech/java/collector.html
ãµã³ãã«ã³ã¼ã
public static void main(String[] args) { ããList<String> list = Arrays.asList("a", "b", "c"); ãã// åç´ã«æååã®çµå String str = list.stream.collect(Collectors.joining()); ããSystem.out.println(str); //abc ãã// åºåãæåã追å ãã¦æååã®çµå String str2 = list.stream.collect(Collectors.joining(":")); ããSystem.out.println(str2); //a:b:c // ãµãã£ãã¯ã¹ããã¬ãã£ãã¯ã¹ã追å ãã¦ã®ã«æååã®çµå String str3 = list.stream.collect(Collectors.joining(":", "[", "]")); ããSystem.out.println(str3); //[a:b:c] }
ã¡ãªã¿ã«Collectorsã¯ã©ã¹ã®ä¸èº«ãè¦ã¦ã¿ãã¨å é¨ã§åºåãæåãããå ´åã¯ãStringBuilderãããªãå ´åã§ãStringJoiner ããå é¨ã§ä½¿ç¨ãã¦ãã¦ãªããªãèå³æ·±ãã£ãã
詳細ï¼GC: Collectors - java.util.stream.Collectors (.java) - GrepCode Class Source
ã¾ã¨ã
æ¢åã®StringBuilderã¨ããå ããã¨ããªãã®ç¨®é¡ã®æ¹æ³ã§æååã®çµåãã§ããããã«ãªã£ããã©ãæçµçã«ä½ã使ãã°ããã®ãã¯çµå±ãã®æã ã§å¤æãããããªãã®ããã
ããããæåã®é£çµãããªãã¨ãããªãå ´åã¯ããã©ã¼ãã³ã¹ãæèãããã®ãé¸æããªãã¨ãããªãããããã§ãªãå ´åãä¾ãã°Listãfilterããããmappingãã延é·ã§æååã«åºåãæåãå ¥ãããå ´åã¯ãã®ã¾ã¾Collectors#joiningã使ãã®ãèªç¶ã ã¨æããã¡ãã£ã¨ããæååã®çµåãªããã©ã¹æ¼ç®åã§é£çµã§ãã¨ãã£ãæãã§ã
ãã ãå人çã«æ®æ®µã®éçºã§ããã¾ã§å¤§ããæ°ã®æååãé£çµããã£ã¦ãã¨ã¯ãã¾ããªãã¨æãããæååã®é£çµãããã¨ãã£ã¦ãã ããããã°ã®åºåå 容ã¨ããã©ããã®ãã¹ã®çæã¨ããå¤ãã¨æãã®ã§ãæååã®é£çµãããã¨ã(åºåãæåãåãè¾¼ã¿ããã¨ã)ã¯ããã®ä¸ã§ä¸çªä½¿ããããStringJoinerã使ããããã¨ãå¤ãã®ããªã¨æã£ãããã¦ããã