How to convert stream to a list in Java

1 Answer

0 votes
import java.util.List;
import java.util.stream.Stream; 
import java.util.stream.Collectors;
  
public class MyClass 
{
    public static void main(String[] args) {
  
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
         
        List<Integer> list = stream.collect(Collectors.toList());
         
        System.out.println(list);
    }
}
 
 
 
 
  
/*
run:
  
[1, 2, 3, 4, 5, 6]
  
*/

 



answered Mar 25, 2023 by avibootz

Related questions

1 answer 136 views
136 views asked May 18, 2020 by avibootz
4 answers 234 views
1 answer 182 views
182 views asked Mar 23, 2023 by avibootz
1 answer 112 views
1 answer 123 views
123 views asked Mar 23, 2023 by avibootz
...