java longstream to string list

To convert a LongStream to a List<String> in Java, you can use the mapToObj method to convert each long element to a String and then collect the stream elements into a list using the collect method. Here's an example:

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class LongStreamToStringListExample {
    public static void main(String[] args) {
        LongStream longStream = LongStream.of(1L, 2L, 3L, 4L, 5L);

        List<String> stringList = longStream
                .mapToObj(String::valueOf) // Convert each long to a String
                .collect(Collectors.toList()); // Collect into a List

        System.out.println(stringList);
    }
}

Output:

[1, 2, 3, 4, 5]

In this example, we create a LongStream using LongStream.of with some example values. Then, we use mapToObj to convert each long element to a String using the String::valueOf method reference. Finally, we collect the stream elements into a List<String> using collect(Collectors.toList()).

댓글

이 블로그의 인기 게시물

Sparse encoder

To build a gRPC service with Gradle, Java, and Avro

Using venv in Python

how to delete doc of datastream in elasticsearch, 엘라스틱서치 데이터스트림 도규먼트 삭제하기

Typescript interface, 타입스크립트 인터페이스

Read a file in Java, 자바 파일 읽기

kubernetes cronjob logs

max_active_runs of Airflow

configure telnet server, 텔넷 서버 설정하기

To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기