LoginSignup
0
0

More than 5 years have passed since last update.

Java8のお勉強(Collector/Collectors)

Posted at

ということでCollector/Collectorsです。
Streamの結果を加工しつつ取り出したい場合なんかに使うみたいです。以下サンプル。

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Collectorsのお勉強
 * @author komikcomik
 *
 */
public class CollectorsHello {

    public static void main(String[] args) {

        // 文字列のストリームを結合してStringにしている、その際に区切り文字と先頭文字、末尾文字を付与している
        Stream<String> s = Stream.of("1", "2", "3", "4", "5", "6", "7");
        String ss = s.collect(Collectors.joining("-", "先頭", "末尾"));
        System.out.println(ss);

    }
}

実行結果は以下の通り。

先頭1-2-3-4-5-6-7末尾

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0