Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Stream APIをちょっと試しに使ってみただけ

More than 5 years have passed since last update.

流行ってるので便乗してちょっと実用性のないコード書いてみただけ。

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        String[] arr = new String[]{"1", "2", "3", "4"};
        List<String> list = new ArrayList<String>(
            Arrays.asList(
                Arrays.stream(arr)
                    .map(x->Integer.parseInt(x)+10)
                    .map(x->String.valueOf(x))
                    .toArray(n->new String[n])));
        for(String s : list){
            System.out.println(s);
        }
    }
}

書いてみた感想:

List<String> list = new ArrayList<String>(
    Arrays.asList(
        Arrays.stream(arr)
            .toArray(n->new String[n])));

のあたりがすごく不格好に見えるのですがこんなもんでしょうかね。toArrayからasListってもっとシンプルに書けそうな。Streamから直接java.util.ArrayList<T>が取れればいいのになと思うのですが。

new ArrayList(Arrays.asList())Stream APIに文句いっても仕方ないので我慢します。

追記

コメントのアドバイスを受けて、時間を測ってみました。

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Stream;
import java.util.stream.Collectors;


public class TS{
    public static final long loop=10000000;
    public static void main(String[] args){
        long laptime;

        laptime = System.currentTimeMillis();
        for(int i=0; i<loop; i++) first_post();
        laptime = System.currentTimeMillis() - laptime;
        System.out.println(laptime+" ms");

        laptime = System.currentTimeMillis();
        for(int i=0; i<loop; i++) users_idea();
        laptime = System.currentTimeMillis() - laptime;
        System.out.println(laptime+" ms");
    }

    public static void first_post(){
        String[] arr = new String[]{"1", "2", "3", "4"};
        List<String> list = new ArrayList<String>(
            Arrays.asList(
                Arrays.stream(arr)
                    .map(x->Integer.parseInt(x)+10)
                    .map(x->String.valueOf(x))
                    .toArray(n->new String[n])));
/*
        for(String s : list){
            System.out.println(s);
        }
*/
    }

    public static void users_idea(){
        String[] arr = new String[]{"1", "2", "3", "4"};
        List<String> list = Stream.of(arr)
                                  .map(x->String.valueOf(Integer.parseInt(x)+10))
                                  .collect(Collectors.toList());
//        list.forEach(System.out::println);
    }
}
user@ubuntu:~$ java8 TS
4877 ms
4441 ms

mapまとめたり、Collectors.toList()に変えたりして結構早くなりました。toArrayからasListする無駄な操作がなくなってコードもスッキリ。ありがとうございます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away